Manoz
Manoz

Reputation: 6587

Change Password length in Membership API

My RegisterModel has the Password Property as below:

 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; }
 }

In Register View, I have this:

<p class="message-info">
Passwords must be at least @Membership.MinRequiredPasswordLength characters long.
</p>

Though, I have changed the MinimumLength of Password to 8 characters in my RegisterModel, still it is showing only 6 on my view page as shown in screenshot below:

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

How to change Password Length?

Upvotes: 1

Views: 2339

Answers (1)

Bhushan Firake
Bhushan Firake

Reputation: 9458

Though you have changed it in your RegisterModel, notice that on your Register View, the value is coming from Membership class that is configured according to your Membership Provider in your web.config file.

So, Check your web.config file. It has the following code:

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

Change minRequiredPasswordLength = "8" here too and it will work for you.

OR

If you don't want to make changes in your Membership Provider, then you can still do this by writing your own Custom Attribute for MinPasswordLength as below:

[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)
        };
    }
}

Then update your RegisterModel to use the MinRequiredPasswordLength DataAnnotation instead.

[Required]        
[MinRequiredPasswordLength(ErrorMessage = "The {0} must be at least {1} character(s) long.")]           
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

Upvotes: 2

Related Questions