1

RegisterModelPassword财产如下:

 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>

虽然,我已将 of 更改为我的MinimumLength中的Password字符8RegisterModel但它仍然只显示6在我的视图页面上,如下面的屏幕截图所示:

这是 MVC 中的默认应用程序,我通过帐户/注册地址访问此页面。我的主机地址是本地的。

如何更改Password长度?

4

1 回答 1

2

尽管您在 中更改了它,但请RegisterModel注意,在您的 上Register View,值来自Membership根据您Membership Providerweb.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以使用MinRequiredPasswordLengthDataAnnotation 代替。

[Required]        
[MinRequiredPasswordLength(ErrorMessage = "The {0} must be at least {1} character(s) long.")]           
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
于 2013-02-06T04:15:09.533 回答