We help IT Professionals succeed at work.

Custom role provider access to web.config settings

Medium Priority
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:

    <add name="myConnectionString"
         connectionString="Data Source={DBSERVER};Initial Catalog={DBNAME};Persist Security Info=True;User ID={USERNAME};Password={PASS}"
         providerName="System.Data.SqlClient" />
    <membership defaultProvider="{NAME}MembershipProvider">
        <add name="{NAME}MembershipProvider"
    <roleManager enabled="true" defaultProvider="{NAME}RoleProvider">
        <add name="{NAME}RoleProvider"
           connectionStringName="myConnectionString" />
    <authentication mode="Forms">
      <forms slidingExpiration="true" timeout="60"  />

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?

Watch Question

Top Expert 2013
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.


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.