Objective: This article gives step-by-step instruction to add Membership files (ASPNETDB.MDF) to an existing MSSQL database.
Background: In ASP.NET 2.0 and above, Microsoft provides drag and drop server controls to quickly create secure Login pages with lots of powerful built-in functions. However, by default, a new and separate MSSQL Server database file is created called (aspnetdb.mdf) which leaves the beginner asp.net developer dumbfounded as to how to incorporate this framework with an already existing database in a multi user environment.
Procedure: Follow instructions below to accomplish this task. This article uses MSSQL Server Express 2005 and Visual Web Developer Express 2008.
- The first step is to have your MSSQL database ready and present in you local database. Create a backup just to be safe and proceed as follows.
- Find and double click on the following executable file: C:\Windows\Microsoft.NET\Framework\v2.(something)\aspnet_regsql.exe.
- You will see the wizard interface like this:
- Click next …
- Make sure the first radio button is selected ( use the second one if you need to remove all membership files from an existing database).
Click Next… - Type in your local serve name (on my system it looks like the image above), and select the database you would like to add the membership files to. Click next, let the wizard finish and you are done.
- Now all the necessary files are added to your existing database so can you start taking advantage of all the powerful features that uncle Bill gave us for absolutely free. There is however one a couple of very important step to complete before you can use the newly added features.
- If you haven’t already, use the add new database connection wizard to your site and make sure it is registered in your web.config file. This is a simple task but if you don’t know how to do it, look for my other article called (Adding a connection string to your site).
- We need to add a bit of code to the web.config file in your site where you intend to use this database. So copy the following code and paste it in at the appropriate location. Navigate to the “authentication” section and make sure you change the mode to “forms” from “windows” like shown below. (authentication mode=“Forms“) If you open the site’s configuration interface by going to the “website” menu on top and click on ASP.NET Configuration, then navigate to the security tab, select authentication type, and choose “From Internet”, it would do the exact same thing. We can save some clicking by simply chaning the authentication mode in the web.config file. Copy and paste the Custom Role Provider and Custom Membership Provider section of the below code into your web.config file and remember to change the connectionStringName=”yourConnectionStringName”. Look at your connection string that was created by the wizard and copy and paste the name into the appropriate sections below.
<!-- The <authentication> section enables configuration of the security authentication mode used by ASP.NET to identify an incoming user. --> <authentication mode="Forms" /> <!-- ................................................................. Custom Role Provider --> <roleManager enabled="true" defaultProvider="CustomizedRoleProvider"> <providers> <add connectionStringName="rpmreLocalConnectionString" name="CustomizedRoleProvider" type="System.Web.Security.SqlRoleProvider" /> </providers> </roleManager> <!-- .................................................................. Custom Membership Provider --> <membership defaultProvider="CustomizedMembershipProvider"> <providers> <add name="CustomizedMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="rpmreLocalConnectionString"/> </providers> </membership>
- That’s it. The task of creating a custom membership provider is now complete.
Take a look at my other articles related to membership to learn more.

No comments yet
Comments feed for this article