代码之家  ›  专栏  ›  技术社区  ›  Matt Brunell

如何测试ASP.NET成员身份密码是否满足配置的复杂性要求?

  •  20
  • Matt Brunell  · 技术社区  · 17 年前

    我有一个ASP.NET页面,允许管理员更改用户的密码。由于管理员不知道用户的密码,因此我使用以下方法:

    MembershipUser member = Membership.GetUser(_usernameTextBox.Text);
    member.ChangePassword(member.ResetPassword(), _passNewTextBox.Text);
    

    --如本文所述 SO question .

    如果新密码不符合web.config文件中配置的复杂性要求,则密码将被重置,但不会更改为所需密码。如果新密码不符合复杂性要求,则密码不应更改。

    是否有一种简单的方法来测试新密码的复杂性要求?

    6 回复  |  直到 8 年前
        1
  •  19
  •   3 revs, 3 users 97%<br/>Bamba&#13; &#13;    13 年前
    /// <summary>
    /// Checks password complexity requirements for the actual membership provider
    /// </summary>
    /// <param name="password">password to check</param>
    /// <returns>true if the password meets the req. complexity</returns>
    static public bool CheckPasswordComplexity(string password)
    {
        return CheckPasswordComplexity(Membership.Provider, password);
    }
    
    
    /// <summary>
    /// Checks password complexity requirements for the given membership provider
    /// </summary>
    /// <param name="membershipProvider">membership provider</param>
    /// <param name="password">password to check</param>
    /// <returns>true if the password meets the req. complexity</returns>
    static public bool CheckPasswordComplexity(MembershipProvider membershipProvider, string password)
    {
        if (string.IsNullOrEmpty(password)) return false;
        if (password.Length < membershipProvider.MinRequiredPasswordLength) return false;
        int nonAlnumCount = 0;
        for (int i = 0; i < password.Length; i++)
        {
            if (!char.IsLetterOrDigit(password, i)) nonAlnumCount++;
        }
        if (nonAlnumCount < membershipProvider.MinRequiredNonAlphanumericCharacters) return false;
        if (!string.IsNullOrEmpty(membershipProvider.PasswordStrengthRegularExpression) &&
            !Regex.IsMatch(password, membershipProvider.PasswordStrengthRegularExpression))
        {
            return false;
        }
        return true;
    }
    
        2
  •  18
  •   Abram Simon    17 年前

    请注意,如果尚未在web.config文件中配置PasswordStrengthRegularExpression属性,则该属性将为空字符串。

    有关正则表达式匹配的信息,请参阅上的MSDN参考 Regex.IsMatch(String)

    *感谢马特的有益评论。

        3
  •  3
  •   Marco    16 年前

    应调整一行以修复小错误。

    修改 if(非算术计数<成员资格.MinRequired非算术字符) if(nonAlnumCount<membershipProvider.MinRequiredNonAlphanumericCharacters)

        4
  •  3
  •   PhilDulac    12 年前

    基于Bamba的解决方案,我决定对成员资格提供程序进行扩展(并减少了代码:

        public static bool IsPasswordValid(this MembershipProvider membershipProvider, string password)
        {
            return (!string.IsNullOrEmpty(password) && // Password is not empty or null AND
                password.Length >= membershipProvider.MinRequiredPasswordLength && // Meets required length AND
                password.Count(c => !char.IsLetterOrDigit(c)) >= membershipProvider.MinRequiredNonAlphanumericCharacters && // Contains enough non-alphanumeric characters AND
                (string.IsNullOrEmpty(membershipProvider.PasswordStrengthRegularExpression) || // Either there is no RegEx requirement OR
                    Regex.IsMatch(password, membershipProvider.PasswordStrengthRegularExpression))); // It matches the RegEx
        }
    

    要使用它,你只需要打电话 Membership.Provider.IsPasswordValid(...) 在需要的地方。

        5
  •  0
  •   Christian C. Salvadó    17 年前

    你也可以使用 Pasword Strength Meter 控制

        6
  •  0
  •   Zachary Yates    17 年前