Los*_*nos 5 c# asp.net-core asp.net-core-identity asp.net-core-2.1
我已经遵循了SO示例代码和官方文档,但是我更改了我的Aspnet核心2.1项目中的密码长度没有任何变化.
我总是收到消息"密码必须至少为6,最多100个字符."
在public void ConfigureServices(IServiceCollection services)我尝试过
services.Configure<IdentityOptions>(options =>
{
options.Password.RequiredLength = 1;
});
Run Code Online (Sandbox Code Playgroud)
在各个地方,或添加到 AddDefaultIdentity<>
services.AddDefaultIdentity<IdentityUser>(options =>
options.Password.RequiredLength = 1;
)
.AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)
但无济于事.
我使用的是非脚手架版本,因此我无法访问HTML或cshtml文件.
看起来您已经找到了搭建脚手架的错误原因。在包含 ASP.NET Core Identity UI 的 Razor Pages 实现的 Razor 类库中,有一个页面InputModel类,Register如下所示:
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; }
...
}
Run Code Online (Sandbox Code Playgroud)
从这段代码片段中可以清楚地看出,无论您设置什么RequiredLength,内置ModelState验证始终需要 6 到 100 个字符的长度。
还值得注意的是,这不仅会影响Register页面 - 我已经确认它还会影响ResetPassword,SetPassword和ChangePassword。
在解决方案方面:Chris Pratt在评论中指出,解决此问题的唯一真正方法是搭建受影响的页面并对StringLength属性进行必要的更改。
更新:您提出的问题已作为副本关闭,解决方案是搭建页面并进行必要的更改。