Random Password Generator

Simple method to generate random passwords in C#.

private static string CHARS_LCASE = "abcdefghijklmnopqrstuvwxyz";
private static string CHARS_UCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static string CHARS_NUMERIC = "1234567890";
private static string CHARS_SPECIAL = "*$-+?_&=!%";

private string GeneratePassword(int length)
{
    string characters = CHARS_LCASE + CHARS_UCASE + CHARS_NUMERIC + CHARS_SPECIAL;

    char[] password = new char[length];

    Random random = new Random();            
    for (int i = 0; i < length; i++)
    {
        int r = random.Next(characters.Length);
        password[i] = characters[r];
    }

    return new string(password);
}
Advertisements

Comments are closed.

%d bloggers like this: