Random password generated in ASP.NET

zhaozj2021-02-12  139

When developing an application that requires a user to register, after the new user submits the registration information, the more common practice is to generate a random password by the program, and then send a password to the electronic mailbox filled in the user. Use the received password to activate its account.

In ASP.NET, the random password generation function is very easy, the following code gives 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

Irandnum = rnd.next (pwdchars.length);

Tmpstr = PWDChars [Irandnum];

}

Return Tmpstr;

}

Talk about the specific ideas:

Method Makepassword accepts two parameters, and the PWDChars parameter specifies which characters that generate the random password string can use, and PWDLEN specifies the length of the generated random code string. With these two parameters, by calling the NEXT () method of the Random class, first obtain an integer greater than or equal to 0 and less than the length of the PWDChars, with the number as the index value, randomly tanked from the available strings to specify The password length is the number of cycles, sequentially connects the obtained character, and finally get the required random password string.

The following code calls the Makepassword () method to get the length of 10, the random string of the available characters is case the usage letters and numbers.

String randomchars = "Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz";

String password = MakePassword (Randomchars, 10);

转载请注明原文地址:https://www.9cbs.com/read-7172.html

New Post(0)