20

我有一个 ASP.NET 页面,它允许管理员更改用户的密码。由于管理员不知道用户的密码,我正在使用以下内容:

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

- 正如这个SO 问题所描述的。

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

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

4

6 回答 6

19
/// <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;
}
于 2009-05-22T11:47:01.107 回答
18

您可以使用以下属性来测试密码:

请注意,如果您尚未在 web.config 文件中配置 PasswordStrengthRegularExpression 属性,它将是一个空字符串。

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

*感谢马特的有用评论。

于 2008-12-17T18:28:17.110 回答
3

我无权访问维基。

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

修改 if (nonAlnumCount < Membership.MinRequiredNonAlphanumericCharacters) 如下 if (nonAlnumCount <membershipProvider.MinRequiredNonAlphanumericCharacters)

于 2010-01-08T22:02:53.137 回答
3

基于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(...)在需要的地方调用。

于 2013-10-02T16:59:49.867 回答
0

您可以使用正则表达式验证器来检查密码是否满足复杂性要求。

您也可以使用密码强度计控件。

于 2008-12-17T18:26:41.123 回答
0

这可能不是最简单的方法,但在页面上使用正则表达式验证器并使其符合密码要求。这样,如果密码不好,您甚至不必回帖。

于 2008-12-17T18:27:31.603 回答