It is a common requirement across most of the online application to generate some random password while registering a new user or for any password reset request in case forgetting an existing password. As of random password, the system will generate a dynamic combination of numbers and alphabets uniquely and send to respective user’s registered Email Address. This way there will be no human interference to sniff the secret password.

The logic to generate a dynamic/random password can be achieved with various ways on server end but the following is the best and short way to get it.

private static Random random = new Random();  

public static string RandomPasswordString(int passwordLength) 
{ 
  string pwdChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
  return new string(Enumerable.Repeat(pwdChars, passwordLength) 
             .Select(s => s[random.Next(s.Length)]).ToArray()); 
}

The first line of declaring random variable should be static and should be global as to make sure we won’t get the same number for any two different requests. 

Hope this short code snippet helps you!!

Happy coding 🙂