【问题标题】:How do I set password options in Aspnet Core 2.1如何在 Aspnet Core 2.1 中设置密码选项
【发布时间】:2019-05-06 08:06:59
【问题描述】:

我遵循了SO example codeofficial documentation,但是我在我的 Aspnet core 2.1 项目中更改了密码长度,没有任何变化。
我总是收到消息“密码必须至少为 6 个字符,最长为 100 个字符。”

public void ConfigureServices(IServiceCollection services)我试过了

services.Configure<IdentityOptions>(options =>
{
  options.Password.RequiredLength = 1;
});

在各个地方,或者添加到AddDefaultIdentity&lt;&gt;

services.AddDefaultIdentity<IdentityUser>(options =>
    options.Password.RequiredLength = 1;
  )
  .AddEntityFrameworkStores<ApplicationDbContext>();

但无济于事。

我使用的是non-scaffolded version,所以我无法访问 HTML 或 cshtml 文件。

【问题讨论】:

  • 在这方面没有任何改变。您发布的代码很好。您遇到的具体问题是什么?
  • 正如@KirkLarkin 所指出的,密码长度由默认 UI 中的某些 Razor 页面的模型单独强制执行。如果您想减少长度要求,您别无选择,只能将至少这些页面脚手架到您的项目中并相应地自定义页面模型。
  • 你试过Manage Nuget Packages’ blogs.msdn.microsoft.com/webdev/2014/01/06/…

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


【解决方案1】:

您似乎找到了一个 bug 理由来搭建脚手架。在包含 ASP.NET Core Identity UI 的 Razor 页面实现的 Razor 类库中,Register 页面有一个 InputModel 类,如下所示:

public class InputModel
{
    ...

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    ...
}

从这段代码 sn-p 可以清楚地看出,无论您将 RequiredLength 设置为什么,内置的 ModelState 验证始终需要 6 到 100 个字符之间的长度。

还值得注意的是,这不仅会影响 Register 页面 - 我已经确认它还会影响 ResetPasswordSetPasswordChangePassword

就解决方案而言:Chris Pratt 在 cmets 中指出,解决此问题的唯一真正方法是搭建受影响的页面并对StringLength 属性进行必要的更改。


更新:您提出的 issue 已作为副本关闭,解决方案是搭建页面并进行所需的更改。

【讨论】:

    【解决方案2】:

    这些都是密码的选项: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity-configuration?view=aspnetcore-2.2

    services.Configure<IdentityOptions>(options =>
    {
        // Default Password settings.
        options.Password.RequireDigit = true;
        options.Password.RequireLowercase = true;
        options.Password.RequireNonAlphanumeric = true;
        options.Password.RequireUppercase = true;
        options.Password.RequiredLength = 6;
        options.Password.RequiredUniqueChars = 1;
    });
    

    要修改规则,请参阅此 How override ASP.NET Core Identity's password policy https://andrewlock.net/creating-custom-password-validators-for-asp-net-core-identity-2/

    更新

    您可以使用PasswordValidator,您可以查看如何使用customize the password policy in ASP.Net identity

    public class ApplicationUserManager : UserManager<ApplicationUser>
      {
               public ApplicationUserManager(): base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
               {
                     PasswordValidator = new MinimumLengthValidator (6);
               }
       }
    

    【讨论】:

    • 这不能回答问题。事实上,OP 确实尝试过(并发布了它)并且正在寻找一种替代方法来设置少于 6 个字符的密码,因为这些选项不适用于这种情况。
    • 看问题可以发现支持ASP.NET Core 2.1, 1.0, 1.1, 2.0, 2.2, 3.0, 3.1, 5.0docs.microsoft.com/en-us/dotnet/api/…,
    • 我已经更新了,希望对你有帮助。
    • 它确实有帮助,非常感谢。有了自定义类就更清楚了。
    猜你喜欢
    • 2019-01-27
    • 2018-06-11
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多