Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Generating Random Passwords with ASP.NET and C#

In this tutorials we will describe you that how we can generate the random password of specific limit. In today' s system, email, net banking online transactions all depend on secure password. Once your password lost or cracked by some one. You may loose a lot of relevant information.

In this example we have taken a function which generates the random password of given length either the length is 8 or 10 or more than 20.


				
						
public static string RandomPassword(int PasswordLength)
{
string _allowedChars="abcdefghijkmnopqrstuvwx
yzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789=+/#$";
Random randNum = new Random();
char[] chars = new char[PasswordLength];
int allowedCharCount = _allowedChars.Length;

for (int i = 0; i < PasswordLength; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length)
* randNum.NextDouble())];
}
return new string(chars);
}

protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
string myInt = TextBox1.Text.ToString();
Label1.Text = "<b>Your generated password is:
</b> " + RandomPassword(int.Parse(myInt));
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Label1.Text = "Please enter a password length (e.g.6)";
}
}

								

Click here to check the Live Example of random generate password.

Share this article   |    Print    |    Article read by 1648 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: