【问题标题】:ASP.NET Core: Using custom Identity Options isn't workingASP.NET Core:使用自定义标识选项不起作用
【发布时间】:2021-03-20 21:52:26
【问题描述】:

我在 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?

【问题讨论】:

  • 身份视图模型也有硬编码的长度属性。但是你在下面说API? UserManager.PasswordValidators 应该只包含这个 IPasswordValidator 服务; github.com/dotnet/aspnetcore/blob/… 应该只关心 IdentityOptions ......所以不知道?

标签: c# .net asp.net-core .net-core asp.net-identity


【解决方案1】:

其实你已经成功配置了密码长度。

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

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

你可以在这里看到InputModel(更改密码的最小长度)。

 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

【讨论】:

  • 因为我没有使用任何 Razor 页面,所以我没有输入模型。该应用程序只是一个 Web API。因此,我在控制器中收到 HTTP POST 请求,并尝试使用:await _userManager.AddPasswordAsync(user, request.Password); 设置用户密码。但在这里我得到了身份错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
  • 2017-12-14
  • 2020-02-10
  • 1970-01-01
相关资源
最近更新 更多