Custom MembershipProvider ConnectionStringName Usage

By Nannette Thacker

This is an issue that seems to confuse a lot of new developers. How do you use the "connectionStringName" defined within the membership provider area of the web.config file within a custom membership provider? Big mouthful, eh? Okay, let's break it down.

Within the web.config file, you may define your connection strings. For our example, below I am setting up a connection string to a SQL Server database within my project's App_Data folder, but it could be a connection string to a remote database on a database server as well.

<connectionStrings>
    <add name="SSSDataMDFConnectionString" 
    connectionString="Data Source=.\SQLEXPRESS;
    AttachDbFilename=|DataDirectory|\SSSDatabase.mdf;
    Integrated Security=True;User Instance=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

In the above example, I have named my connection string SSSDataMDFConnectionString. Now I want to setup my custom Membership Provider in the system.web section of my web.config:

<membership defaultProvider="SSSMembershipProvider">
      <providers>
        <clear/>
        <add name="SSSMembershipProvider" 
        type="SSSMembershipProvider" 
        requiresQuestionAndAnswer="false" 
        enablePasswordRetrieval="true" 
        enablePasswordReset="true" 
        description="Custom Membership Provider" 
        requiresUniqueEmail="true" 
        applicationName="/" 
        passwordFormat="clear" 
        userIsOnlineTimeWindow="15" 
        connectionStringName="SSSDataMDFConnectionString"/>
      </providers>
    </membership>

In the above example, I have inserted the connection string name within the "connectionStringName" property. Note that if your custom membership provider is defined within a namespace, to be sure to add that to the name of the provider itself. For instance, if your namespace is "SSS" then you would add the namespace to the definition:

<membership defaultProvider="SSS.SSSMembershipProvider">
      <providers>
        <clear/>
        <add name="SSS.SSSMembershipProvider" 
        type="SSS.SSSMembershipProvider" 

Now let's look at a few snippets from our custom membership provider class. In the snippet below, notice I have defined the connection string variable.

Public Class SSSMembershipProvider
    Inherits MembershipProvider

  Public connStr As String
        

Typically, you may obtain the value of various configuration file settings with the use of config():

config("enablePasswordReset")

However, we need to use the ConfigurationManager.ConnectionStrings Property to obtain the configuration setting. Within the initialize function of the class I now retrieve the value of the actual connection string that is associated with the name defined in the web.config:

Public Overrides Sub Initialize(ByVal name As String, _
ByVal config As System.Collections.Specialized.NameValueCollection)
        
connStr = ConfigurationManager.ConnectionStrings(config("connectionStringName")).ConnectionString

The above is a very long line, and just in case it is cut off on the right, I will break it on 2 lines so you don't miss it:

connStr = ConfigurationManager.ConnectionStrings(
config("connectionStringName")).ConnectionString

Now you're all set to use this in your class functions for accessing the connection:

Using conn As New SqlConnection(connStr)
    conn.Open()

May your dreams be in ASP.NET!

Nannette Thacker

3 Comments

Comments have been disabled for this content.