I want to create a random password generator in my ASP.NET C# web application.
I have created that but it is applicable only for the integer format. If I give any characters it shows the debugging error as “my input format is wrong”.
But i want to enter both string and integer. For example my textbox field may be
dft-001
I want a random password for this value and also my length should be restricted to 8 digits or characters only.
My current code:
public static string CreateRandomPassword(int PasswordLength)
{
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789-";
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)
{
userid.Text = "Your id is: " + id.Text;
if(id .Text!="")
{
string myInt = id.Text.ToString();
password.Text = "Your password is: " + CreateRandomPassword(int.Parse(myInt));
}
}
There are a few things you can do to correct this based on what you have stated in your requirements.
If I’ve understood what you want to do correctly, then this should get you there.
Ref: MSDN Random.Next