Analysis on ASP. NET Generation of random passwords

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.