create a password with symbols in csharp

Here is an example of a method to create a random string password using symbols:

main.cs
public static string GeneratePassword(int length)
{
    const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_-+=[{]};:<>|./?";
    var random = new Random();
    var chars = new char[length];
    for (var i = 0; i < length; i++)
    {
        chars[i] = validChars[random.Next(0, validChars.Length)];
    }
    return new string(chars);
}
386 chars
12 lines

This method generates a secure password with the specified length, by selecting random characters from a set of valid characters that include letters, digits, and symbols. The length of the password must be specified as a parameter to the method.

gistlibby LogSnag