Home > ASP.NET > ASP.NET – Read membership section of Web.Config file from the code

ASP.NET – Read membership section of Web.Config file from the code

Hi Geeks,

Today i came across the scenario where i was using the MembershipProvider authentication for the security issues and i required the values of some keys of the membership section of web.config files at the code/server side.

-> i have my membership section of web.config file as follows

<membership>
      <
providers> <clear/> <add name="LDAPMembershipProvider" type="System.Web.Security.SqlMembershipProvider"
             connectionStringName="ApplicationServices" IP="12.45.999.10"
              PORT="252"
              RootUserName="xyz"
              RootPassword="pqr"
              BaseSearchString="ou=sss,DC=aaa,DC=COM"
              FilterKeyName="ck"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false" maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </
providers>
    </
membership>

and I needed the values of Port,IP,RootUsername etc at server side.

-> Following is the code which gave me the membership section value at server side.

var section = (MembershipSection)ConfigurationManager.GetSection("system.web/membership");
       string IP = Convert.ToString(section.Providers["LDAPMembershipProvider"].Parameters["IP"]);
       int Port = Convert.ToInt32(section.Providers["LDAPMembershipProvider"].Parameters["PORT"]);
       string RootUserName = Convert.ToString(section.Providers["LDAPMembershipProvider"].Parameters["RootUserName"]);
       string RootPassword = Convert.ToString(section.Providers["LDAPMembershipProvider"].Parameters["RootPassword"]);
       string BaseSearchString = Convert.ToString(section.Providers["LDAPMembershipProvider"].Parameters["BaseSearchString"]);
       string FilterKeyName = Convert.ToString(section.Providers["LDAPMembershipProvider"].Parameters["FilterKeyName"]);

 

Thanks.

Advertisements
Categories: ASP.NET
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: