asp.net mvc 5 identity 2.0 注册时密码强度验证
密码强度验证分两部分,一部分是客户端,即浏览器中进行验证,另一部分是 identity 的 UserManager 中进行的。
密码强度先在浏览器端进行简单的验证,该部分代码使用 HtmlHelper+DataAnnotation 实现,在view model中相应属性上使用不同的data anotation,htmlhelper可以生成对应效果的html。
在 UserManager 中验证的部分通过配置 UserManager 的 PasswordValidator 实现,通常在继承类的 Create 方法中为其赋值一个新的 PasswordValidator。
data annotations
代码:
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
解释:
* Required - 限定不能为空白
* StringLength - 限定最小长度
* Compare - 比较两个字符串是否相同
这些 特性并不能直接产生效果,是通过 HtmlHelper 解释后才得到我们想要的效果。
identity
在 App_Start/IdentityConfig.cs文件中 ApplicationUserManager.Create 静态方法中:
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 8,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
1249




被折叠的 条评论
为什么被折叠?



