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