您可以在MVC中添加密码验证的地方吗? [英] Places you can add password validation in MVC?

查看:72
本文介绍了您可以在MVC中添加密码验证的地方吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在"ForgotPassword"之后有一个"ForceChangePassword"页面,它在"NewPassword"字段上随机抛出两个长度要求.问题是,我从未添加长度要求7,也不知道它来自哪里或如何更改.

I've got a "ForceChangePassword" page for after "ForgotPassword" and it is randomly throwing two length requirements on the "NewPassword" field. The problem is, I never added a length requirement of 7 and have no idea where it is coming from or how to change it.

来自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.

我通过属性在ViewModel上将长度要求设置为6(请参见下文).我不知道这7个要求从哪里来.

I set the length requirement to 6 on the ViewModel via Attribute (see below). I have no idea where the 7 requirement is coming from.

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>

罪犯属性:

data-val-password-min="7"

问题:在使用Identity 2.0的MVC 5项目中添加密码长度验证的可能位置在哪里?我可能没有设置某种默认要求吗?

Question: Where are the possible locations to add password-length validation in an MVC 5 project using Identity 2.0? Could there be a default-requirement of some sort I'm just not setting?

注意:如有必要,我将添加更多代码,但我已发布了我所知道的所有可能相关的代码.别无其他提及密码(据我所知,如果还有更多位置,请告诉我).

Note: I'll add more code if necessary but I've posted all possibly relevant code I know of. Nothing else mentions passwords at all (to my knowledge, let me know if there are more locations).

推荐答案

找到了它.我想MembershipPassword属性的默认长度为7.它之所以具有双倍长度要求,是因为该属性具有长度要求,而我也有一个StringLength属性.因此,这两者都引发了错误.

Found it. The MembershipPassword attribute had a default of 7 length I guess. It was having double length requirement simply because that attribute has a length requirement and I had a StringLength attribute too. So it threw errors for both.

修复:

[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属性)

这篇关于您可以在MVC中添加密码验证的地方吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆