如何验证密码是否包含X个大写字母和Y个数字?

发布于 12-20 03:08 字数 58 浏览 1 评论 0原文

在C#中如何验证密码至少包含X个大写字母和至少Y个数字,并且整个字符串比Z长?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼2024-12-27 03:08:26

密码强度:

首先,我会阅读密码强度,并仔细检查您的策略,以确保您做的是正确的事情(我无法立即告诉您):

然后我会检查其他问题:

然后我就开始说正事了。

实现:

您可以使用 Linq:

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

您还可以使用正则表达式(这可能是一个不错的选择,可以让您将来插入更复杂的验证):

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

或者您可以混合和匹配它们。

如果您必须多次执行此操作,您可以通过构建一个类来重用 Regex 实例:

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);

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):

Then I'd check other questions:

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);
才能让你更想念2024-12-27 03:08:26

要计算大写字母和数字:

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++;
}

并检查 s.Length 的长度

祝你好运!

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!

国粹2024-12-27 03:08:26

使用 LINQ Where() 方法 简洁明了:

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;

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;
彼岸花ソ最美的依靠2024-12-27 03:08:26

这应该可以做到:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文