Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Custom role provider access to web.config settings

Posted on 2009-02-17
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?

Question by:Anubis2005
1 Comment
LVL 41

Accepted Solution

guru_sami earned 2000 total points
ID: 23661837
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


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question