ASP.NET 核心:使用自定义身份选项不起作用

ASP.NET Core: Using custom Identity Options isn't working

我在 ASP.NET Core Web API 应用程序中使用 Identity,并希望使用自定义 Identity 选项。我像这样注册身份时将它们传递到 Startup.cs 中:

Action<IdentityOptions> configureOptions = options =>
   {
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 4;
    options.Password.RequireLowercase = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireNonAlphanumeric = false;
    options.SignIn.RequireConfirmedAccount = true;
    options.SignIn.RequireConfirmedEmail = true;
  };

services
 .AddIdentityCore<ApplicationUser>(configureOptions)
 .AddDefaultTokenProviders()
 .AddEntityFrameworkStores<ApplicationDbContext>()
 .AddErrorDescriber<CustomIdentityErrorDescriber>();

但是这些选项不会改变任何东西。

例如,当我将密码要求的最小长度设置为 4 并添加一个长度为 5 的用户密码时,我得到一个 IdentityError,表明最小长度为 8。

这对我来说似乎很奇怪,因为 documentations of Identity 声明默认值为 6?

那么8是从哪里来的,为什么不是我配置的4?

事实上,您已经成功配置了密码长度。

由于Identity默认密码长度为6位,如果设置为4位,则验证不通过

您可以在以下位置找到您的登录模型:

你可以在这里看到InputModel(更改密码的MinimumLength)。

 public class InputModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }
        //You can change the MinimumLength to 4.
        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 4)]
        [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; }
    }

不知道为什么你的默认长度是8,可能你之前修改过吧

测试结果:

更新:做我下面的步骤,然后找到InputModel