How to verify that the password contains X uppercase letters and Y numbers?

前端 未结 4 2140
温柔的废话
温柔的废话 2021-02-09 01:31

How do I verify in C# that the password contains at least X uppercase letters and at least Y numbers, and the entire string is longer than Z?

Thanks.

相关标签:
4条回答
  • 2021-02-09 01:52

    Short and clear using LINQ Where() method:

    int requiredDigits = 5;
    int requiredUppercase = 5;
    string password = "SomE TrickY PassworD 12345";
    
    bool isValid = password.Where(Char.IsDigit).Count() >= requiredDigits
                   && 
                   password.Where(Char.IsUpper).Count() >= requiredUppercase;
    
    0 讨论(0)
  • 2021-02-09 01:55

    Password Strength:

    First, I would read up on password strength, and double-check your policy to make sure you were doing the right thing (I couldn't tell you off hand):

    • http://en.wikipedia.org/wiki/Password_strength
    • https://www.grc.com/haystack.htm
    • http://xkcd.com/936/ (a joke, but good food for thought)

    Then I'd check other questions:

    • Creating a regex to check for a strong password
    • Password strength

    Then I'd get down to business.

    Implementation:

    You could use Linq:

    return password.Length >= z
        && password.Where(char.IsUpper).Count() >= x
        && password.Where(char.IsDigit).Count() >= y
        ;
    

    You could use also regular expressions (which might be a good option to allow you to plug in more complicated validations in the future):

    return password.Length >= z
        && new Regex("[A-Z]").Matches(password).Count >= x
        && new Regex("[0-9]").Matches(password).Count >= y
        ;
    

    Or you could mix and match them.

    If you had to do this multiple times, you could reuse the Regex instances by building a class:

    public class PasswordValidator
    {
        public bool IsValid(string password)
        {
            return password.Length > MinimumLength
                && uppercaseCharacterMatcher.Matches(password).Count
                    >= FewestUppercaseCharactersAllowed
                && digitsMatcher.Matches(password).Count >= FewestDigitsAllowed
                ;
        }
    
        public int FewestUppercaseCharactersAllowed { get; set; }
        public int FewestDigitsAllowed { get; set; }
        public int MinimumLength { get; set; }
    
        private Regex uppercaseCharacterMatcher = new Regex("[A-Z]");
        private Regex digitsMatcher = new Regex("[a-z]");
    }
    
    var validator = new PasswordValidator()
    {
        FewestUppercaseCharactersAllowed = x,
        FewestDigitsAllowed = y,
        MinimumLength = z,
    };
    
    return validator.IsValid(password);
    
    0 讨论(0)
  • 2021-02-09 01:57

    This should do it:

    public bool CheckPasswordStrength(string password, int x, int y, int z)
    {
       return password.Length >= z &&
              password.Count(c => c.IsUpper(c)) >= x &&
              password.Count(c => c.IsDigit(c)) >= y;
    }
    
    0 讨论(0)
  • 2021-02-09 02:08

    To count uppercase letters and digits:

    string s = "some-password";
    int upcaseCount= 0;
    int numbersCount= 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (char.IsUpper(s[i])) upcaseCount++; 
        if (char.IsDigit(s[i])) numbersCount++;
    }
    

    and check s.Length for the length

    good luck!

    0 讨论(0)
提交回复
热议问题