我在PasswordValidator上设置了多个属性,但唯一检查的规则是密码长度.
public static ApplicationUserManager Create(IdentityFactoryOptionsoptions, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore (context.Get ())); // Configure validation logic for passwords manager.PasswordValidator = new PasswordValidator() { RequiredLength = int.Parse(ConfigurationManager.AppSettings["PasswordRequiredLength"]), RequireNonLetterOrDigit = bool.Parse(ConfigurationManager.AppSettings["PasswordRequireNonLetterOrDigit"]), RequireDigit = bool.Parse(ConfigurationManager.AppSettings["PasswordRequireDigit"]), RequireLowercase = bool.Parse(ConfigurationManager.AppSettings["PasswordRequireLowercase"]), RequireUppercase = bool.Parse(ConfigurationManager.AppSettings["PasswordRequireUppercase"]) };
这几乎是从ASP.NET Identity示例应用程序复制的,除了值来自config而不是硬编码.
当我查看PasswordValidator()的定义时,我看到这些属性都被定义了(当然它编译并运行).但是,当我测试更改密码时,我注意到只有长度导致验证错误.AccountController有来自示例的代码:
IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
为了更好看,我补充道
IdentityResult resultPassword = await UserManager.PasswordValidator.ValidateAsync(model.NewPassword); if (resultPassword.Succeeded) { IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
我注意到userManager.PasswordValidator属于MinimumLengthValidator类,而不是我认为我们开始使用的PasswordValidator.当然,这解释了这种行为,但我无法确定如何做到这一点
正确地将"完整"PassordValidator添加到我的userManager和/或
访问PassordValidator的属性(例如RequireDigit,RequireUppercase等)
请注意,我试过了
PasswordValidator myPV = (PasswordValidator)UserManager.PasswordValidator;
这导致无法转换类型为"Microsoft.AspNet.Identity.MinimumLengthValidator"的对象以键入"Microsoft.AspNet.Identity.PasswordValidator"错误.
最好,斯科特