我在“ForgotPassword”之后有一个“ForceChangePassword”页面,它在“NewPassword”字段上随机抛出两个长度要求。问题是,我从未添加7的长度要求,也不知道它来自何处或如何更改它。
来自ModelState的错误
The New password must be at least 6 characters long.
The 'New password' field is an invalid password. Password must have 7 or more characters.
if (!ModelState.IsValid) { return View(model); } // Only line executed in the POST Action.
我通过Attribute在ViewModel上将长度要求设置为6(见下文)。我不知道7要求来自哪里。
IdentityConfig.cs
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
模型
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[MembershipPassword(
MinRequiredNonAlphanumericCharacters = 1,
MinNonAlphanumericCharactersError = "Your password needs to contain at least one symbol (!, @, #, etc).",
ErrorMessage = "Your password must be 6 characters long and contain at least one symbol (!, @, #, etc)."
)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
查看
<div class="form-group">
@Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.NewPassword, new { @class = "form-control", @autocomplete = "off" })
</div>
</div>
Culprit属性:
data-val-password-min="7"
问题:使用Identity 2.0在MVC 5项目中添加密码长度验证的可能位置在哪里?可能有某种类型的默认要求我只是没有设置?
注意:我会在必要时添加更多代码,但我已经发布了所有可能相关的代码。没有其他人提到密码(据我所知,如果有更多地点,请告诉我。)
答案 0 :(得分:3)
找到它。我猜MembershipPassword
属性的默认值为7。它具有双倍长度要求仅仅因为该属性具有长度要求而且我也具有StringLength
属性。所以它为两者都犯了错误。
<强>修正:强>
[Required]
[MembershipPassword(
MinRequiredNonAlphanumericCharacters = 1,
MinNonAlphanumericCharactersError = "Your password needs to contain at least one symbol (!, @, #, etc).",
ErrorMessage = "Your password must be 6 characters long and contain at least one symbol (!, @, #, etc).",
MinRequiredPasswordLength = 6
)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
(设置MinRequiredPasswordLength
并删除StringLength
属性)
答案 1 :(得分:2)
在App_Start\IdentityConfig.cs
中,出现以下代码:
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
这就是你需要改变的。 [MembershipPassword]
来自较旧的ASP.NET成员身份验证,它与Identity完全不同并且独立。