We help IT Professionals succeed at work.

Custom role provider access to web.config settings

Medium Priority
1,309 Views
Last Modified: 2013-12-17
Hello All,

I'm in the process of creating a custom role/membership provider but I'm needing to know how I can access the settings of the provider as defined in the web.config file.

Example web.config:

<configuration>
  <connectionStrings>
    <add name="myConnectionString"
         connectionString="Data Source={DBSERVER};Initial Catalog={DBNAME};Persist Security Info=True;User ID={USERNAME};Password={PASS}"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="{NAME}MembershipProvider">
      <providers>
        <clear/>
        <add name="{NAME}MembershipProvider"
             type="AS.Security.Providers.Membership"
             connectionStringName="myConnectionString"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="true"
             applicationName="{APPLICATION_NAME}"
             requiresUniqueEmail="true"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="7"
             minRequiredNonalphanumericCharacters="1"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression=""/>
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="{NAME}RoleProvider">
      <providers>
        <clear/>
        <add name="{NAME}RoleProvider"
           type="AS.Security.Providers.Role"
           applicationName="{APPLICATION_NAME}"
           connectionStringName="myConnectionString" />
      </providers>
    </roleManager>
    <authentication mode="Forms">
      <forms slidingExpiration="true" timeout="60"  />
    </authentication>
  </system.web>
</configuration>


What I want to know, from within my custom provider, how can I find out what the connection string is that is defined in the web.config from within my custom class?

Thanks
Anubis.
Comment
Watch Question

Top Expert 2013
Commented:
In the initialize Method of your Custom Membership provider you can get all attributes you have in web.config from the config collection  passed as parameter to this method.
Check this: http://www.devx.com/asp/Article/29256/0/page/3
Sample code:
 

//Global variable or you can set a connectionString property in your custom membership provider class.
 
string connStr;
 
//Initialize method 
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) 
{ 
/* Here we retrieve the attribute values set 
in web.config and assign to local variables */ 
base.Initialize(name, config); 
ConnectionStringSettings pConnectionStringSettings = ConfigurationManager.ConnectionStrings[config["connectionString"]]; 
connStr = pConnectionStringSettings.ConnectionString; 
}

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.