posted 2/24/2009 by Vijendra Shakya
Here we discuss how to generate the random password.Random password increase the security of website it simply provide the non easily guessable password.Here we generate a set of random digits by combining a set of alphabets and numbers and special characters.After generating the random passsword it is the combination of string,number and special characters.One of the simplest way to generate the random password via GUID(Globally Unigue ID),it is a 128 bit number producing a hexadecimal number.following is a sample code to generate random password via GUID.
public string RandomPassword(int length){ string randomPass= Guid.NewGuid().ToString(); randomPass= randomPass.Replace("-",string.Empty); // this random password contains all character in uppercase. //if you want ur random password contains all character in lower case then use .ToLower() return randomPass.Substring(0,length).ToUpper(); } In this NewGuid().ToString() returns a string of 32 hexadecimals (excluding the "-").
Another way to generate the random number is :
public string RandomPassword(int length) { // give character which you want in random generated password. char[] allowCharacter = "abcdefgijkmnopqrstwxyzABCDEFGHJKLMNPQRSTWXYZ23456789*$-+?_&=!%{}/".ToCharArray(); // create object of random Random RandChar = new Random(); string randomPassword = ""; for (int i = 0; i < length; i++) { randomPassword += allowCharacter[RandChar.Next(0, allowCharacter.Length)]; } //.ToUpper()for your password is in Uppercase and ToLower() password is in Lower case. return randomPassword.ToUpper(); }
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18