IDEA: Centralize Configuration for Membership Provider

Jul 10, 2012 at 9:04 PM
Edited Jul 11, 2012 at 7:24 AM

In my project I needed a place where to centralize the settings for the Membership Provider so I created the file 'Membership/CodeFirstMembershipConfiguration.cs' and put the following constants into it:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace CodeFirstMembershipSharp
{
    public class CodeFirstMembershipConfiguration
    {
        public const int MaxInvalidPasswordAttempts = 3;
        public const int MinRequiredNonAlphanumericCharacters = 2;
        public const int MinRequiredPasswordLength = 8;
        public const int PasswordAttemptWindow = 0;
        public const MembershipPasswordFormat PasswordFormat = MembershipPasswordFormat.Hashed;
        public const string PasswordStrengthRegularExpression = "";
        public const bool RequiresUniqueEmail = true;
    }
}

 

Then I modified the properties of 'Membership/CodeFirstMembershipProvider.cs':

 

        public override int MaxInvalidPasswordAttempts
        {
            get { return CodeFirstMembershipConfiguration.MaxInvalidPasswordAttempts; }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { return CodeFirstMembershipConfiguration.MinRequiredNonAlphanumericCharacters; }
        }

        public override int MinRequiredPasswordLength
        {
            get { return CodeFirstMembershipConfiguration.MinRequiredPasswordLength; }
        }

        public override int PasswordAttemptWindow
        {
            get { return CodeFirstMembershipConfiguration.PasswordAttemptWindow; }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { return CodeFirstMembershipConfiguration.PasswordFormat; }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { return CodeFirstMembershipConfiguration.PasswordStrengthRegularExpression; }
        }

        public override bool RequiresUniqueEmail
        {
            get { return CodeFirstMembershipConfiguration.RequiresUniqueEmail; }
        }

 

Then I replaced the MinimumLength for the 'New Password' properties in file 'Models/AccountModel.cs' from beeing hard-coded like:

 

        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

 

to beeing dynamic like:

 

        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = CodeFirstMembershipConfiguration.MinRequiredPasswordLength)]

 

I thought maybe modifying the provider like this by default would add some flexibility in the DRY-way, so people who want to modify the information dont necessarily need to browse to different locations.

Well, its just an idea!

Anyways, CFMP rocks, I love it!

 

Ingo

 

 

Coordinator
Jul 11, 2012 at 7:19 AM
Edited Jul 11, 2012 at 7:19 AM

Good idea,

you could have also put this in the WebSecurity.cs as the class is already static.

but the thing is with NuGet I cant modify the Models/AccountModel.cs, so each time you File -> New Project you would need to do this yourself.

Good little tutorial for the users though.

Jul 11, 2012 at 7:28 AM

Well the changes to AccountModel.cs are small... maybe if the package is installed, there could be some hint/message to modify the 'MinimumLength' in AccountModel.cs like there is (is it?) to replace some web.config stuff with the lines form membership.config

Coordinator
Jul 11, 2012 at 8:36 AM

That's the part id like to get rid of, but the differences between MVC3 and MVC4 beta's and MVC4 RC makes it hard to do. So after MVC4 comes out, there will be no membership.config and this provider will be MVC4 only, and no user interaction will be necessary to work.