Asp.Net Identity 2中的密码验证

时间:2018-12-17 12:14:50

标签: c# asp.net-mvc asp.net-identity

我的应用程序的密码格式在IdentityConfig.cs中实现,如下所示:

manager.PasswordValidator = new PasswordValidator
{
    RequiredLength = 6,
    RequireNonLetterOrDigit = true,
    RequireDigit = true,
    RequireLowercase = true,
    RequireUppercase = true,
};

但是,验证消息是“密码必须至少包含一个非字母或数字字符”,这对于大多数用户而言并不清楚。

我想更改它,因此我添加了一个自定义验证,如下所示:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class PasswordAttribute : ValidationAttribute
{
    public override string FormatErrorMessage(string name)
    {
        return "The password should be a combination of upper case, lower case letters, digits and special characters";
    }
    public override bool IsValid(object value)
    {
        if (value == null) return false;

        if (value.ToString().Length == 8)
            return true;
        else
            return false;
    }
}

我在模型中也提到了以下验证:

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[CustomValidations.Password]
public string Password { get; set; }

但是,密码验证消息仍然与以前相同。我想念什么?如何更改密码字段的默认验证消息?

是否可以在IdentityConfig.cs中修改验证消息?

0 个答案:

没有答案