Generate a Random Password String in C#

Generate a Random Password String in C#

C# Code Snippets

C#: Get Random Password String

This is just a simple way to get a random string for generated passwords.

private static string GetRandomPassword(int passwordLength)
{
    // return 0-9, a-z, A-Z. No punctuation chars. Add what you need.

    string charactersAllowed = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
    char[] chars = new char[passwordLength];
    Random rand = new Random();

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

    return new string(chars);
}

Example: string pw1 = YourClassName.GetRandomPassword(8);