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

时间:2015-06-19 15:50:33

标签: asp.net-mvc asp.net-identity asp.net-core asp.net-core-mvc

在使用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<>()

但我无法使用。

1 个答案:

答案 0 :(得分:21)

解决方案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();