我创建了一个密码验证器,如下所示:
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 8,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = true,
RequireUppercase = true,
};
但是,测试123之类的密码被视为无效:
密码必须至少包含一个非字母或数字字符。
错误信息非常不清楚,它说密码应该至少有一个非字母或数字字符。好吧,我有3位数。那有什么不对呢?当我添加一个非字母
答案 0 :(得分:12)
此属性的名称和documentation都不明确。这应该是:
获取或设置密码是否需要不是a的字符 字母而不是数字。
答案 1 :(得分:1)
public class CustomPasswordValidator : PasswordValidator
{
public override async Task<IdentityResult> ValidateAsync(string password)
{
var requireNonLetterOrDigit = base.RequireNonLetterOrDigit;
base.RequireNonLetterOrDigit = false;
var result = await base.ValidateAsync(password);
if (!requireNonLetterOrDigit)
return result;
if (!Enumerable.All<char>((IEnumerable<char>)password, new Func<char, bool>(this.IsLetterOrDigit)))
return result;
// Build a new list of errors so that the custom 'PasswordRequireNonLetterOrDigit' could be added.
List<string> list = new List<string>();
foreach (var error in result.Errors)
{
list.Add(error);
}
// Add our own message: (The default by MS is: 'Passwords must have at least one non letter or digit character.')
list.Add("Passwords must have at least one character that is neither a letter or digit. (E.g. '£ $ % ^ _ etc.')");
result = await Task.FromResult<IdentityResult>(IdentityResult.Failed(string.Join(" ", (IEnumerable<string>)list)));
return result;
}
}