【问题标题】:Altering ASP.NET Core Services DefaultIdentity does not alter the password options更改 ASP.NET Core 服务 DefaultIdentity 不会更改密码选项
【发布时间】:2019-09-08 06:30:55
【问题描述】:

在脚手架身份验证之后,我很难更改身份选项。

我使用了这个命令:

dotnet new webapp --auth Individual -o mywebapp

然后,我更改了 Startup.cs 文件,更改了身份选项:

 public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlite(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<IdentityUser>(config =>
        {
            // TODO
            //config.SignIn.RequireConfirmedEmail = true;
        })
            .AddDefaultUI(UIFramework.Bootstrap4)
            .AddEntityFrameworkStores<ApplicationDbContext>();


        services.Configure<IdentityOptions>(options =>
        {
            // Password settings
            options.Password.RequireDigit = true;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequireLowercase = false;
            options.Password.RequiredLength = 3;
            options.Password.RequiredUniqueChars = 1;

            // Lockout settings
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
            options.Lockout.MaxFailedAccessAttempts = 5;
            options.Lockout.AllowedForNewUsers = true;

            // User settings
            options.User.RequireUniqueEmail = true;
        });

设置options.Password.RequiredLength 无效。

当我运行应用程序时,浏览器显示错误消息:

The Password must be at least 6 and at max 100 characters long.

我没有预料到错误“至少 6”;从我读到的应该是“3”。 我该如何解决这个问题?

这么多的黑魔法正在发生,搜索“至少”的框架什么也没有发现。

编辑---> 添加搭建好的 ViewModel 代码:

namespace netplus.Models.Account
{
   public class RegisterViewModel
   {
      [Required, MaxLength(256)]
      public string Username { get; set; }

      [Required, DataType(DataType.Password)]
      public string Password { get; set; }

      [DataType(DataType.Password), Compare(nameof(Password))]
      public string ConfirmPassword { get; set; }
   }
}

【问题讨论】:

  • @KirkLarkin 是的,看起来确实如此,并且在链接到 Github 之后,将错误显示为“Identity ignoring RequiredLength #774”,并且日期为 2016 年 3 月 16 日。想知道它是否真的是重复的因为在 asp.net core 2.2.0 中还没有修复。
  • 我对您配置项目的方式并不完全清楚,所以我也不确定它是否重复。我不明白你为什么有RegisterViewModel 以及它来自哪里。您描述的错误消息来自“默认用户界面”,所以我认为这很可能是同一个问题。
  • @KirkLarkin,我开始了一个简单的项目(使用 Mac Visual Studio),新的解决方案,Web Application MVC .Net Core。我进入命令行并运行此命令以添加身份验证“dotnet new webapp --auth Individual”,然后我运行项目并尝试更改所需的密码长度,因为这只会妨碍我手动测试;然后我注意到控制密码长度的选项没有效果。
  • 这听起来像是我链接的同一个问题,@TaoZhou 已经回答了 - 你显示的命令不应该生成这样的 RegisterViewModel 类,所以这有点这里令人困惑。

标签: c# asp.net-core


【解决方案1】:

对于IdentityOptions,配置为_userManager.CreateAsync(user, Input.Password)验证密码。

对于The Password must be at least 6 and at max 100 characters long,这由RegisterModel 中的ViewModel 控制。

您无法使用IdentityOptions 来控制此客户端验证。

按照以下步骤控制它:

  1. 右键项目->Add New Scaffold Item->Identity->Check Account\Register->右击Data context class
  2. 打开RegisterModel并修改InputModel进行客户端验证

    public class InputModel
    {
        [Required]
        [EmailAddress]
        [Display(Name = "Email")]
        public string Email { get; set; }
    
        [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; }
    
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }
    

【讨论】:

  • 嗨,涛,感谢您展示代码。我已经更新了问题以显示当前的 ViewModel。奇怪的是用户名没有 [EmailAddress] 属性,密码也没有定义属性,但是当我运行应用程序时,我注意到用户名必须是电子邮件地址,密码必须是 6 个字符长。仍然想知道“默认”的来源。
  • 应用属性后,[Display(Name = "Magic")];输出是一样的。似乎所有属性都对 UI 没有影响。想知道如果使用“.AddDefaultUI(UIFramework.Bootstrap4)”会不会有效果。
  • @Wayne 您目前的RegisterModel PageModel 是什么?请注意,我的步骤是添加Identity Scaffold,而不仅仅是普通类。
  • 没有“RegisterModel”,只有“RegisterViewModel”。我使用此命令生成代码:“dotnet new webapp --auth Individual”
  • @Wayne 你关注Right click project -&gt;Add New Scaffold Item-&gt; Identity-&gt;Check Account\Register-&gt;Select right Data context class了吗?您当前的 .net 核心 sdk 是什么?
猜你喜欢
  • 2019-05-01
  • 2011-06-28
  • 1970-01-01
  • 2011-05-20
  • 2015-05-31
  • 1970-01-01
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多