C# generate random password


C# function to generate random password. You can customize this as per your need.

public static string RandomPassword()
{
    var chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
    var password = string.Empty;
    var random = new Random();

    for (var i = 0; i < 8; i++)
    {
        var x = random.Next(1, chars.Length);
        if (!password.Contains(chars.GetValue(x).ToString()))
        {
            password += chars.GetValue(x);
        }
        else
        {
            i--;
        }
    }

    return password;
}

By changing loop length in above function you can change size of password.