当前位置:  开发笔记 > 编程语言 > 正文

ASP.NET身份密码验证

如何解决《ASP.NET身份密码验证》经验,为你挑选了2个好方法。

我在我的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 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}是什么意思?谢谢.



1> Alkasai..:

Startup.cs添加Identity Service的位置,您可以添加密码验证选项:

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



2> 小智..:

如果您使用其中一个ASP.NET模板应用程序并选择身份验证作为"个人用户帐户",您将在应用程序的App_Start文件夹中的IdentityConfig.cs文件中找到密码选项.您可以在此处更改密码选项,以关闭除密码长度以外的所有要求:

manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = false,
            RequireUppercase = false,
        };

推荐阅读
臭小子
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有