问题描述
ASP.NET 5 中提供的默认身份提供程序默认有非常严格的密码规则,需要一个小写字符、一个大写字符、一个非字母数字字符和一个数字.我正在寻找一种方法来更改提供商的密码要求.
The default Identity provider provided in ASP.NET 5 has very strict password rules by default, requiring a lower case character, an upper case character, a non-alphanumeric character, and a number. I am looking for a way to change the password requirements for the provider.
以前在 ASP.NET 4 中,可以通过 Web.config XML 文件配置提供程序,如 以前回答过.但是 ASP.NET 5 使用新的基于代码的配置模式,不清楚如何配置身份.
Previously in ASP.NET 4, the provider could be configured via the Web.config XML file, as previously answered. However ASP.NET 5 uses the new code based configuration pattern and it is unclear how to configure the identity.
如何更改我的应用程序的密码要求?
How can I change the password requirements for my application?
推荐答案
我实际上最终弄清楚了这一点,事实证明您需要为 AddDefaultIdentity 提供一个合适的 lambda 表达式来配置它提供的 IdentityOptions.这是在 Startup 类的 ConfigureServices 方法中完成的,如下所示:
I actually ended up figuring this out, it turns out you need to supply AddDefaultIdentity with a suitable lambda expression that configures the IdentityOptions it provides. This is done inside the ConfigureServices method within the Startup class, like so:
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 中它略微更改为:
The above was true in the beta1 versions of the framework, in the latest rc1 beta5 it has changed slightly to:
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();
这篇关于如何在 ASP.NET 5 MVC 6 (vNext) 中定义 Identity 的密码规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!