Applications that require user registration before using the provided functions ProgramWhen a new user submits the registration information, a common practice is to generate a random password by the program, and then send the password to the e-mail address entered during user registration, the user activates the account with the received password.
It is easy to implement the ASP. NET random password generation function. The followingCodeA complete implementation method is provided:
Publicstaticstringmakepassword (stringpwdchars, intpwdlen)
{
Stringtmpstr = "";
Intirandnum;
Randomrnd = newrandom ();
For (INTI = 0; I {
Irandnum = RND. Next (pwdchars. Length );
Tmpstr + = pwdchars [irandnum];
}
Returntmpstr;
} Compare the source code with the specific ideas:
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.