Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:54:49+00:00 2026-06-06T06:54:49+00:00

So in MVC when using the .NET Membership system, password policies are defined in

  • 0

So in MVC when using the .NET Membership system, password policies are defined in the web.config file. For example minPasswordLength is defined in membership->profiles.

When using a View this is accessible using the @Membership component

Passwords must be at least @Membership.MinRequiredPasswordLength characters long.

However if you look at the default model in the example MVC Application it says

 [Required]
 [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
 [DataType(DataType.Password)]
 [Display(Name = "New Password")]
 public string NewPassword { get; set; }

The part I am curious about is the MinimumLength = 6 since this is hard coded it would mean that if I ever wanted to update the password length, I’d not only have to edit the web.config (like Microsoft suggest) but also search any references to it in the source and change all over the place (probably not the best programming practise).

Is there any ways of using variables in Attributes. I suspect not since this probably happens at compile time rather than runtime. If there isn’t does anyone know of a better pattern to stop me having to find replace all references in the future?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:54:50+00:00Added an answer on June 6, 2026 at 6:54 am

    Here is an article that can help you answer you questions. Basically, create your own DataAnnotation that pulls the minimum length from the web.config.

    For posterity, here is the code used in the referenced site:

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

    and on your ViewModel

    [Required]        
    [MinRequiredPasswordLength(ErrorMessage = "The {0} must be at least {1} character(s) long.")]           
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am extending the default asp.net mvc example.. i am using asp.net membership provider
I have a ASP.NET MVC site using Membership Provider. I have trouble testing some
I recently started looking into building web applications using .NET MVC and I stumbled
I'm using asp.net 4.0 with asp.net MVC 2.0 and the asp.net membership provider. I
I am building a library using mvc, mongodb and asp.net membership. When a user
I am using asp.net MVC with the asp.net membership but I starting to think
I am using ASP.NET MVC with ASP.NET membership. Following best practices for 'I forgot
I am using asp.net mvc. I have generated a view that retrieve all unapproved
i am using the asp.net membership that ships with asp.net-mvc sample app. how do
I'm using ASP.NET MVC 1.0 along with the Oracle ASP.NET Membership Providers. I'm running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.