尽管您在 中更改了它,但请RegisterModel
注意,在您的 上Register View
,值来自Membership
根据您Membership
Provider
在web.config
文件中配置的类。
所以,检查你的web.config
文件。它有以下代码:
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="ApplicationServices"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6" //Change This
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
在这里也改变minRequiredPasswordLength = "8"
,它会为你工作。
或者
如果您不想在 中进行更改Membership Provider
,那么您仍然可以通过编写自己的自定义属性来执行此操作,MinPasswordLength
如下所示:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter , AllowMultiple = false, Inherited = true)]
public sealed class MinRequiredPasswordLengthAttribute : ValidationAttribute, IClientValidatable
{
private readonly int _minimumLength = Membership.MinRequiredPasswordLength;
public override string FormatErrorMessage(string name)
{
return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, _minimumLength);
}
public override bool IsValid(object value)
{
string password = value.ToString();
return password.Length >= this._minimumLength;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new[]{ new ModelClientValidationStringLengthRule(FormatErrorMessage(metadata.GetDisplayName()), _minimumLength, int.MaxValue)
};
}
}
然后更新您RegisterModel
以使用MinRequiredPasswordLength
DataAnnotation 代替。
[Required]
[MinRequiredPasswordLength(ErrorMessage = "The {0} must be at least {1} character(s) long.")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }