我在我的MVC项目中使用Identity,它一切都很好。除了注册新用户的表单有一些疯狂的密码要求之外
密码必须至少包含一个非字母或数字字符。密码必须至少有一位
('0'-'9')
。密码必须至少包含一个大写('A'-'Z')
。
这是注册模型
public class RegisterViewModel
{
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[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 = "Passord")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Repeat Password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
帐户控制器
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
我看到了密码的长度要求,但我不明白如何更改密码验证,因此我不需要非字母或数字字符。
非常感谢所有人,谢谢。
奖励:{0}
和{2}
是什么意思?谢谢。
答案 0 :(得分:12)
在添加身份服务的Startup.cs
中,您可以添加密码验证选项:
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration,
options =>
options.Password = new PasswordOptions
{
RequireDigit = true,
RequiredLength = 6,
RequireLowercase = true,
RequireUppercase = true,
RequireNonLetterOrDigit = false
})
[...];
答案 1 :(得分:7)
如果您使用其中一个ASP.NET模板应用程序并选择身份验证为“个人用户帐户”,您将在应用程序的App_Start文件夹中的IdentityConfig.cs文件中找到密码选项。您可以在此处更改密码选项,以关闭密码长度以外的所有要求:
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};