Sharing ASP.NET security Database between different applications

This blog will mention the steps that is required to share the asp.net security database between different applications.

The steps to follow :

  1. Since you are going to use one database for many applications , it will not be practical to have the database(aspnet.mdf) placed in App_Data folder of one web site.Instead Create a new database to hold the security tables ,Please follow This post on how to create that database and populate it with the required database tables.
  2. Now you have the required  security tables in  shared database , you need to tell each application to use it's own data(users,roles,profiles...).This can be accomplished by specifying the ApplicationName for Membership configuration section.

This is an example on how to configure one web site  to use the shared security database( assuming the database name is "MembershipDataBase").

<connectionStrings>
    <remove name="LocalSqlServer"/>
    <add name="LocalSqlServer" connectionString="Initial Catalog=MembershipDataBase;Data Source=.\sqlexpress;Integrated Security=SSPI;Persist Security Info=False;"/>
</connectionStrings>
<system.web>
    <roleManager enabled="true" />
    <membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
        <providers>
            <clear />
            <add
              name="SqlMembershipProvider"
              type="System.Web.Security.SqlMembershipProvider"
              connectionStringName="LocalSqlServer"
              applicationName="Website1"
              />
        </providers>
    </membership>

 

Note:Notice how I set the applicationName attribute to "website1" , this value will be used by the Providers to get the data of the current application and to avoid mixing application data.( data here means : users,profiles,Roles,personalization ....)

Now , if you want to use the same security database for another website, you need to change applicationName value from Website1 to any other value (for simplicity you may choose the website name for that value )

 

Hope it helps.

No Comments