int randomNumber = 0; string password = "";the randomNumber will be the variable that will hold the decision number. And the password variable is the one that will hold the password. Then we will create a random object:
Random rnd = new Random();rnd will generate a number and assign it to randomNumber. After that we write the loop that will generate the password.
for (int i = 1; i <= 13; i++) { randomNumber = rnd.Next(1,5); if (randomNumber == 1) password += rnd.Next(1, 10); else if (randomNumber == 2) password += Convert.ToChar(rnd.Next(65, 92)); else if (randomNumber == 3) password += Convert.ToChar(rnd.Next(97, 123)); else if (randomNumber == 4) password += Convert.ToChar(rnd.Next(33, 46)); }
- randomNumber = rnd.Next(1, 5); will generate an integer number between 1 and 5 (5 is not included) and will assign it to randomNumber.
- Then we will check the value of randomNumber against if else block. If randomNumber ==1 then we will generate a number between 1 and 9
- if randomNumber ==2 we will generate a number between 65 and 91 and as you know these numbers represent the corresponding ASCII values for uppercase alphabets A-Z then we will convert it to a character using Convert.ToChar() method
- If randomNumber ==3 we will generate a lowercase character a-z and lastly if randomNumber ==4 will generate one of these special characters !"#$%'()*+/-
here is the full code:
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace TestConsole { class Program { static void Main(string[] args) { int randomNumber = 0; string password = ""; Random rnd = new Random(); for (int i = 1; i <= 13; i++) { randomNumber = rnd.Next(1,5); if (randomNumber == 1) password += rnd.Next(1, 10); else if (randomNumber == 2) password += Convert.ToChar(rnd.Next(65, 91)); else if (randomNumber == 3) password += Convert.ToChar(rnd.Next(97, 123)); else if (randomNumber == 4) password += Convert.ToChar(rnd.Next(33, 46)); } Console.WriteLine("Your new password is {0}", password); Console.ReadLine(); } } }
output:
A typical procedure to change the user's password:
- If the user demanded a new password using his email, first check if the email is exist in the database
- If the email is valid:
- Generate a new password
- Encrypt it by converting it to a hash digest
- Replace the hash digest (of the new password) with the old one in the database
- Send the new password to the user
0 comments:
Post a Comment