Change Password length in Membership API

前端 未结 1 1287
时光说笑
时光说笑 2021-01-07 07:41

My RegisterModel has the Password Property as below:

 public class RegisterModel
 {  
    [Required]
    [StringLength(100, ErrorMe         


        
1条回答
  •  隐瞒了意图╮
    2021-01-07 07:49

    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:

    
      
        
        
      
    
    

    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 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; }
    
    • See more at: http://timjames.me/mvc-3-password-length-dataannotation#sthash.kxyHoPep.dpuf

    0 讨论(0)
提交回复
热议问题