gpt4 book ai didi

asp.net-mvc - ASP.NET 身份密码验证

转载 作者:行者123 更新时间:2023-12-02 00:11:59 25 4
gpt4 key购买 nike

我在我的 MVC 项目中使用 Identity,一切都很好。除了注册新用户的表单有一些疯狂的密码要求之外

Passwords must have at least one non letter or digit character. Passwords must have at least one digit ('0'-'9'). Passwords must have at least one uppercase ('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; }
}

帐户 Controller

    // 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} 是什么意思?谢谢。

最佳答案

在添加身份服务的 Startup.cs 中,您可以添加密码验证选项:

services.AddIdentity<ApplicationUser, IdentityRole>(Configuration, 
options =>
options.Password = new PasswordOptions
{
RequireDigit = true,
RequiredLength = 6,
RequireLowercase = true,
RequireUppercase = true,
RequireNonLetterOrDigit = false
})
[...];

关于asp.net-mvc - ASP.NET 身份密码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com