Asp.net-Mvc

如何在 MVC6 或 AspNet Core 或 IdentityCore 中更改 PasswordValidator

  • June 15, 2016

在使用 Identity 的 Asp.Net MVC 5 中,可以執行以下操作:

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

如何在 MVC 6 中更改相同的配置?

我看到可以在該段中的 ConfigurationServices 方法中:

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddPasswordValidator<>()

但我無法使用。

解決方案 Beta6

Startup.cs編寫程式碼中:

         services.ConfigureIdentity(options =>
           {
               options.Password.RequireDigit = false;
               options.Password.RequiredLength = 6;
               options.Password.RequireLowercase = false;
               options.Password.RequireNonLetterOrDigit = false;
               options.Password.RequireUppercase = false;
           });

更新 Beta8 和 RC1

           // Add Identity services to the services container.
           services.AddIdentity<ApplicationUser, IdentityRole>(options =>
              {
                  options.Password.RequireDigit = false;
                  options.Password.RequiredLength = 6;
                  options.Password.RequireLowercase = false;
                  options.Password.RequireNonLetterOrDigit = false;
                  options.Password.RequireUppercase = false;
              })
               .AddEntityFrameworkStores<ApplicationDbContext>()
               .AddDefaultTokenProviders();

更新 RC2

           // Add Identity services to the services container.
           services.AddIdentity<ApplicationUser, IdentityRole>(options =>
              {
                  options.Password.RequireDigit = false;
                  options.Password.RequiredLength = 6;
                  options.Password.RequireLowercase = false;
                  options.Password.RequireNonAlphanumeric= false;
                  options.Password.RequireUppercase = false;
              })
               .AddEntityFrameworkStores<ApplicationDbContext>()
               .AddDefaultTokenProviders();

引用自:https://stackoverflow.com/questions/30942325