Reading membership section from web.config
Asked Answered
F

3

7

I have created a custom MembershipProvider class, so far so good, however, I am not sure how to read the configuration settings from the web.config file.

I tried to search from Google and Stackoverflow, seems like someone also experiencing my problem and asked, but no answer has been given.

It should be a simple thing, but I am very new in web development, so reading settings from web.config seems away too technical to me.

Here is my settings:

 <membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear/>
    <add name="CustomMembershipProvider"
         type="Test.Models.CustomMembershipProvider,Test"
         passwordFormat="Hashed"
         connectionStringName="ApplicationServices"
         minRequiredPasswordLength="8"
         minRequiredNonalphanumericCharacters="0"
         maxInvalidPasswordAttempts="5"
         enablePasswordReset="false"
         enablePasswordRetrieval="false"
         requiresQuestionAndAnswer="false"
         applicationName="/"/>
  </providers>
</membership>

I would like to read the minRequiredPasswordLength setting, please assist.

Fascinating answered 8/6, 2010 at 11:26 Comment(0)
D
10

As this is set as your default provider it should be enough to:

int i = Membership.MinRequiredPasswordLength;

And that would return an int specifying the minimum required password length.

Dielle answered 8/6, 2010 at 11:31 Comment(1)
Sorry to add to this, but how about the PasswordFormat section?Loire
R
21

here is the solution in code :

        MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
        string defaultProvider = membershipSection.DefaultProvider;
        ProviderSettings providerSettings = membershipSection.Providers[defaultProvider];
        string connectionStringName = providerSettings.Parameters["connectionStringName"];
        string connectionUsername = providerSettings.Parameters["connectionUsername"];
        string connectionPassword = providerSettings.Parameters["connectionPassword"];
        string connectionString = WebConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
Rupe answered 21/7, 2010 at 17:51 Comment(1)
+1 Thanks, I was looking for a way to load the default provider from config.Hulen
D
10

As this is set as your default provider it should be enough to:

int i = Membership.MinRequiredPasswordLength;

And that would return an int specifying the minimum required password length.

Dielle answered 8/6, 2010 at 11:31 Comment(1)
Sorry to add to this, but how about the PasswordFormat section?Loire
V
2

If you have overridden the System.Web.Security.MembershipProvider in your own class you can get the web.config membership settings as Robban suggests, just by calling the System.Web.Security.Membership methods. However, these calls will be directed to your membership provider class, so you will need to provide some implementation.

Supposing you have overridden the MembershipProvider class and added a section in the config file, as in the original question above. A call to int i = Membership.MinRequiredPasswordLength will be directed to YOUR implementation. This might look like this:

   public override int MinRequiredPasswordLength
    {
        get { return _minRequiredPasswordLength; }
    }

MSDN gives a complete example here. The example shows you how to read the config file to set the local properties like _minRequiredPasswordLength.

Vivia answered 3/4, 2012 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.