Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code for password generator

I'm working on a C# project where I need to generate random passwords.

Can anyone provide some code or a high-level approach for password generation?

It should be possible to specify the following:

  1. Minimum password length
  2. Maximum password length
  3. Valid uppercase characters
  4. Minimum number of uppercase chars
  5. Valid lowercase chars
  6. Minimum number of lowercase chars
  7. Valid numeric chars
  8. Minimum number of numeric chars
  9. Valid alfanum chars.
  10. Minimum number of alfanum chars
like image 824
Allan Simonsen Avatar asked Mar 09 '11 08:03

Allan Simonsen


People also ask

How do I get a random password?

Avoid weak, commonly used passwords like asd123, password1, or Temp!. Some examples of a strong password include: S&2x4S12nLS1*, JANa@sx3l2&s$, 49915w5$oYmH. Avoid using personal information for your security questions, instead, use LastPass to generate another “password" and store it as the answer to these questions.


1 Answers

you can use this method and modify according to your need

private static string CreateRandomPassword(int passwordLength)
{
 string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
 char[] chars = new char[passwordLength];
 Random rd = new Random();

 for (int i = 0; i < passwordLength; i++)
 {
  chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
 }

 return new string(chars);
}
like image 69
Muhammad Akhtar Avatar answered Oct 29 '22 17:10

Muhammad Akhtar