如何在 Aspnet Core 2.1 中设置密码选项
How do I set password options in Aspnet Core 2.1
我遵循了 and the official documentation 但是我在我的 Aspnet 核心 2.1 项目中更改了密码长度,没有任何变化。
我总是收到消息 "The Password must be at least 6 and at max 100 characters long."
在public void ConfigureServices(IServiceCollection services)
我试过了
services.Configure<IdentityOptions>(options =>
{
options.Password.RequiredLength = 1;
});
在各个地方,或者添加到AddDefaultIdentity<>
services.AddDefaultIdentity<IdentityUser>(options =>
options.Password.RequiredLength = 1;
)
.AddEntityFrameworkStores<ApplicationDbContext>();
但无济于事。
我正在使用 ,所以我无法访问 HTML 或 cshtml 文件。
您似乎找到了 错误 脚手架的理由。在包含 ASP.NET Core Identity UI 的 Razor Pages 实现的 Razor Class 库中,Register
页面有一个 InputModel
class看起来像这样:
public class InputModel
{
...
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
...
}
从该代码片段可以清楚地看出,无论您将 RequiredLength
设置为什么,内置 ModelState
验证始终需要 6 到 100 个字符的长度。
还值得注意的是,这不仅会影响 Register
页面 - 我已经确认它还会影响 ResetPassword
、SetPassword
和 ChangePassword
。
在解决方案方面:Chris Pratt 在评论中指出,解决此问题的唯一真正方法是构建受影响的页面并对 StringLength
属性进行必要的更改。
更新:您提出的 issue 已作为副本关闭,解决方案是构建页面并进行必要的更改。
services.Configure<IdentityOptions>(options =>
{
// Default Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
});
要修改规则,请参阅此
https://andrewlock.net/creating-custom-password-validators-for-asp-net-core-identity-2/
更新
您可以使用 PasswordValidator
并且您可以了解如何 customize the password policy in ASP.Net identity
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(): base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
PasswordValidator = new MinimumLengthValidator (6);
}
}
我遵循了
我总是收到消息 "The Password must be at least 6 and at max 100 characters long."
在public void ConfigureServices(IServiceCollection services)
我试过了
services.Configure<IdentityOptions>(options =>
{
options.Password.RequiredLength = 1;
});
在各个地方,或者添加到AddDefaultIdentity<>
services.AddDefaultIdentity<IdentityUser>(options =>
options.Password.RequiredLength = 1;
)
.AddEntityFrameworkStores<ApplicationDbContext>();
但无济于事。
我正在使用
您似乎找到了 错误 脚手架的理由。在包含 ASP.NET Core Identity UI 的 Razor Pages 实现的 Razor Class 库中,Register
页面有一个 InputModel
class看起来像这样:
public class InputModel
{
...
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
...
}
从该代码片段可以清楚地看出,无论您将 RequiredLength
设置为什么,内置 ModelState
验证始终需要 6 到 100 个字符的长度。
还值得注意的是,这不仅会影响 Register
页面 - 我已经确认它还会影响 ResetPassword
、SetPassword
和 ChangePassword
。
在解决方案方面:Chris Pratt 在评论中指出,解决此问题的唯一真正方法是构建受影响的页面并对 StringLength
属性进行必要的更改。
更新:您提出的 issue 已作为副本关闭,解决方案是构建页面并进行必要的更改。
services.Configure<IdentityOptions>(options =>
{
// Default Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
});
要修改规则,请参阅此
更新
您可以使用 PasswordValidator
并且您可以了解如何 customize the password policy in ASP.Net identity
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(): base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
{
PasswordValidator = new MinimumLengthValidator (6);
}
}