如何在ASP.NET 5 MVC 6(vNext)中定义身份的密码规则?


76

默认情况下,ASP.NET 5中提供的默认身份提供程序具有非常严格的密码规则,要求使用小写字母,大写字母,非字母数字字符和数字。我正在寻找一种更改提供商密码要求的方法。

以前在ASP.NET 4中,可以通过Web.config XML文件配置提供程序,如先前所回答。但是,ASP.NET 5使用基于新代码的配置模式,目前尚不清楚如何配置身份。

如何更改应用程序的密码要求?


2
如果您想深入了解密码要求(也许应该),NIST(标准委员会)最近推出了新的密码指南。请参阅此处的摘要文章:passwordping.com/surprising-new-password-guidelines-nist以及完整的NIST标准出版物。请参阅第5.1.1.2节:pages.nist.gov/800-63-3/sp800-63b.html#sec3
弗朗西斯科·德安科尼亚地区

Answers:


138

实际上,我最终弄清楚了这一点,事实证明,您需要为AddDefaultIdentity提供一个合适的lambda表达式,以配置它提供的IdentityOptions。这是在Startup类的ConfigureServices方法内完成的,如下所示:

public class Startup {
    public void ConfigureServices(IServiceCollection services) {

        // Add Identity services to the services container.
        services.AddDefaultIdentity<ApplicationIdentityDbContext, ApplicationUser, IdentityRole>(Configuration,
            o => {
                o.Password.RequireDigit = false;
                o.Password.RequireLowercase = false;
                o.Password.RequireUppercase = false;
                o.Password.RequireNonLetterOrDigit = false;
                o.Password.RequiredLength = 7;
            });
    }
}

更新2:

上面的内容在框架的beta1版本中是正确的,在最新的rc1 beta5中,它已稍微更改为:

services.AddIdentity<ApplicationUser, IdentityRole>(o => {
    // configure identity options
    o.Password.RequireDigit = false;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationIdentityDbContext>()
.AddDefaultTokenProviders();

4
是的,或者您可以直接调用services.ConfigureIdentity(o => {});
郝宫

2
真好!您认为可以通过该config.json文件吗?
Dave Van den Eynde 2015年

@DaveVandenEynde我刚刚尝试过。不工作。下方的json。“ Identity”:{“ Password”:{“ RequireDigit”:“ false”,“ RequireLowercase”:“ false”,“ RequiredLength”:“ 5”,“ RequireUppercase”:“ false”,“ RequireNonLetterOrDigit”:“ false”} }
nVentimiglia

从我对MVC 6的了解中,我认为应该由程序员在应用程序启动时通过从config显式读取它来使想法可配置。
Dave Van den Eynde 2015年

1
也可以看看这种在初始化后设置选项的解决方案:stackoverflow.com/a/30942723/1507481
Jannik Arndt

27

如果您使用以下方法建立了一个新的Web项目,Individual User Accounts请转到:

App_Start -> IdentityConfig.cs

您可以在其中编辑以下默认值:

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

11

在startup.cs中:

   services.AddIdentity<ApplicationUser, IdentityRole>(x =>
        {
            x.Password.RequiredLength = 6;
            x.Password.RequireUppercase = false;
            x.Password.RequireLowercase = false;
            x.Password.RequireNonAlphanumeric = false;
        }).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

这就是Core 2.0的答案。谢谢。
horseman1210

8

我想要做的是自定义密码规则,以便它应包含以下至少两个组中的字符:小写,大写,数字和特殊符号

我不能仅通过更改PasswordValidator选项来执行以下操作:

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

因此,我改为通过扩展IIdentityValidator来创建自定义验证器...

首先,在您的Extensions文件夹中创建一个新文件CustomPasswordValidator.cs:

public class CustomPasswordValidator : IIdentityValidator<string>
{
    public int RequiredLength { get; set; }
    public CustomPasswordValidator(int length) {
        RequiredLength = length;
    }

    /* 
     * logic to validate password: I am using regex to count how many 
     * types of characters exists in the password
     */
    public Task<IdentityResult> ValidateAsync(string password) {
        if (String.IsNullOrEmpty(password) || password.Length < RequiredLength)
        {
            return Task.FromResult(IdentityResult.Failed(
                String.Format("Password should be at least {0} characters", RequiredLength)));
        }

        int counter = 0;
        List<string> patterns = new List<string>();
        patterns.Add(@"[a-z]");                                          // lowercase
        patterns.Add(@"[A-Z]");                                          // uppercase
        patterns.Add(@"[0-9]");                                          // digits
        // don't forget to include white space in special symbols
        patterns.Add(@"[!@#$%^&*\(\)_\+\-\={}<>,\.\|""'~`:;\\?\/\[\] ]"); // special symbols

        // count type of different chars in password
        foreach (string p in patterns)
        {
            if (Regex.IsMatch(password, p))
            {
                counter++;
            }
        }

        if (counter < 2)
        {
            return Task.FromResult(IdentityResult.Failed(
                "Please use characters from at least two of these groups: lowercase, uppercase, digits, special symbols"));
        }

        return Task.FromResult(IdentityResult.Success);
    }
}

然后转到IdentityConfig.cs,并在Create方法中对其进行初始化:

manager.PasswordValidator = new CustomPasswordValidator(8 /*min length*/);
        /*
        // You don't need this anymore
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };
        */

有关更多详细信息,请参见我的教程


2
我觉得这对用户来说是最好的解决方案。这样可以在给用户灵活性的同时实现合理的安全级别。而不是打开或关闭某些限制,这使用户可以使用密码来选择他们想要的内容。
弗朗西斯科·德·安科尼亚'18
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.