如何在 ASP.NET 5 MVC 6 (vNext) 中定义身份的密码规则?
How do I define the password rules for Identity in ASP.NET 5 MVC 6 (vNext)?
ASP.NET 5 中提供的默认身份提供程序在默认情况下具有非常严格的密码规则,要求一个小写字符、一个大写字符、一个非字母数字字符和一个数字。我正在寻找一种方法来更改提供商的密码要求。
以前在 ASP.NET 4 中,可以通过 Web.config XML 文件配置提供程序,如 previously answered。但是ASP.NET5使用了新的基于代码的配置模式,不清楚如何配置身份。
如何更改我的应用程序的密码要求?
我实际上最终弄清楚了这一点,事实证明您需要为 AddDefaultIdentity 提供一个合适的 lambda 表达式来配置它提供的 IdentityOptions。这是在 Startup class 的 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();
在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();
如果您已经使用 Individual User Accounts
设置了一个新的 Web 项目,请转到:
App_Start -> IdentityConfig.cs
在那里您可以编辑以下默认值:
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
我想做的是自定义密码规则,使其包含至少以下组中的 2 个字符:小写、大写、数字和特殊符号.
这不是我可以通过更改 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(
$"Password should be at least {RequiredLength} characters"));
}
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 and 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,
};
*/
有关详细信息,请参阅 my tutorial。
ASP.NET 5 中提供的默认身份提供程序在默认情况下具有非常严格的密码规则,要求一个小写字符、一个大写字符、一个非字母数字字符和一个数字。我正在寻找一种方法来更改提供商的密码要求。
以前在 ASP.NET 4 中,可以通过 Web.config XML 文件配置提供程序,如 previously answered。但是ASP.NET5使用了新的基于代码的配置模式,不清楚如何配置身份。
如何更改我的应用程序的密码要求?
我实际上最终弄清楚了这一点,事实证明您需要为 AddDefaultIdentity 提供一个合适的 lambda 表达式来配置它提供的 IdentityOptions。这是在 Startup class 的 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();
在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();
如果您已经使用 Individual User Accounts
设置了一个新的 Web 项目,请转到:
App_Start -> IdentityConfig.cs
在那里您可以编辑以下默认值:
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
我想做的是自定义密码规则,使其包含至少以下组中的 2 个字符:小写、大写、数字和特殊符号.
这不是我可以通过更改 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(
$"Password should be at least {RequiredLength} characters"));
}
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 and 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,
};
*/
有关详细信息,请参阅 my tutorial。