在Membership API中更改密码长度

时间:2013-02-06 04:10:02

标签: .net asp.net-mvc asp.net-mvc-4

我的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如下面的截图所示:

This is default application in MVC , I am accessing this page by account/register address.My host address is local.

如何更改Password长度?

1 个答案:

答案 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