我的RegisterModel
拥有Password
属性,如下所示:
public class RegisterModel
{
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
}
在注册视图中,我有这个:
<p class="message-info">
Passwords must be at least @Membership.MinRequiredPasswordLength characters long.
</p>
尽管如此,我已将MinimumLength
中的Password
8
更改为RegisterModel
个字符,但我的视图页面上仅显示6
如下面的截图所示:
如何更改Password
长度?
答案 0 :(得分:2)
虽然您已在RegisterModel
中对其进行了更改,但请注意,Register View
上的值来自Membership
类,该类是根据您的Membership
{{ 1}}在您的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
DataAnnotation。
MinRequiredPasswordLength