Tuesday, December 30, 2008

Create a Random Password


Some times we need to create a random password policy in our website, After some research create a class that will create a random password .We are using System.Security.Cryptography namespace in this class.

public static string CreateRandomPassword(int PasswordLength)

{

String _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789";
Byte[] randomBytes = new Byte[PasswordLength];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
char[] chars = new char[PasswordLength];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < PasswordLength; i++)
{
chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
}
return new string(chars);
}

You can use this class as a random password generator,This is best practice to create a password policy in your application.
For more detail about System.Security.Cryptography do not forgot to knock msdn.

comments

1 Response to "Create a Random Password"
  1. Anonymous said...
    December 30, 2008 at 5:03 AM

    Ultimate Help

 

Copyright 2009 All Rights Reserved Shakti Singh Dulawat