Generate a random password using ASP. NET
When developing an application that requires user registration to use the provided functions, after the new user submits the registration information, a common practice is to generate a random password by the program, then, send the password to the email address entered during user registration, and then use the password you received to activate your account.
It is easy to implement ASP. NET to generate a random password. The following code provides a complete implementation method:
Public static string MakePassword (string pwdchars, int pwdlen)
{
String tmpstr = "";
Int iRandNum;
Random rnd = new Random ();
For (int I = 0; I <pwdlen; I ++)
{
IRandNum = rnd. Next (pwdchars. Length );
Tmpstr + = pwdchars [iRandNum];
}
Return tmpstr;
}
Let's talk about the specific ideas based on the source code:
The MakePassword method accepts two parameters. The pwdchars parameter specifies which characters can be used for the generated random password string, and pwdlen specifies the length of the generated random password string. With these two parameters, call the Next () method of the Random class to obtain an integer greater than or equal to 0 and less than the length of pwdchars, using this number as the index value, take random characters from available strings, take the specified password length as the number of cycles, connect the obtained characters in sequence, and finally obtain the required random password string.
The Code calls the MakePassword () method to obtain a random string with a length of 10 and a range of uppercase/lowercase letters and numbers.
String randomchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string password = MakePassword (randomchars, 10); ASP. NET generates random passwords.