Custom role provider access to web.config settings

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

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now