C# Programming for Beginners | Integrating SQLMembershipProvider with Existing Database

What is the SQLMembership Provider?

Basically the SQL Membership Provider provides membership and roles for your websites automatically. It does this by installing a database that can enable you to help users to register and login easily, allowing you to spend more time on building your website and adding content.  This is instead of trying to implement a custom designed membership database, which is time consuming.

You need to run the aspnet_regsql tool in order to install the membership and database.  This tool can be found at the path C:\ >> Windows >> Microsoft.NET >> Framework >> v2.0 >> aspnet_regsql.  This is a command line tool that can be run from the command prompt as well.  However if you double-click it, it will run a wizard - this is what I used.






I ran the aspnet_regsql tool first up in order to use the SQLMembershipProvider.  This gives you the opportunity to add the provider using an existing database or to create a new (default) database.  If you use the default database the tool will create a database called ASPDBNET.mdf.  I had an existing database already and so used Windows Authentication and the server name as: localhost\SqlExpress, then selected my database from the dropdown window.  Following all the prompts I finished the wizard.

Now, if you open up Sql Server Express (I use SQL Management Studio Express) and navigate to your database you will see that the tool has added a whole lot of database tables all starting with aspnet_.  These are the tables that are needed for the SQL Membership Provider.  So far so good.

One more thing to remember to do is to set your authentication mechanism to forms authentication.  Look for the section that is called <system.web>.  Directly below that add the following line: (or change it if it is set to “Windows”)

ASP.NET SAMPLE:


 <authentication mode="Forms" />


Now we need to add a new user in order to test the login controls.
Open the ASP.NET Configuration for your site.  The icon for this is a small red hammer next to a blue/green earth on the top of the Solution Explorer.  




Once this opens click on the security tab and select “Create User”.  Fill in the textboxes – be sure to write down the passwords and usernames.

Now go back into Visual Studio and add the login control and the CreateUserWizard to your application, add a loginname control as well.  All these controls are found in the Data section of the Toolbox in Visual Studio.  Click the green arrow to run the ASP.NET application.  Then test the login control.  If you login correctly the loginname control should display your login name.  It should work fine.

When I attempted to use the SQLMembershipProvider in order to use the built in login control and CreateUserWizard –– I found that the controls worked well, I could log in and create new users with no hassle.  However, when I went into Sql Management Studio and ran a query on the table in my database, I could not find any of the records of the new users (The query was very basic:  SELECT * FROM aspnet_User).  What gives?

Well after a lot of fiddling and testing I found that the entries that are made in the web.config file for the provider set the default provider to the default database – ASPDBNET.mdf  When you run your application and register a new user, this then creates another database in your App_Data folder.  Even though you told the regsql tool to create the membership database in your existing database, it has created the default database when you registered a new user as well.  It then writes the new user details to the database in your App_Data folder and not your existing database in Sql Express.

So how do we sort out this problem?
Go into your web.config and find the <connectionStrings/> section.  Here you need to add the details of your connection string to your SQL database. 

ASP.NET SAMPLE:

<connectionStrings>
        <add name="YourConnectionStringName" connectionString="Data Source=YourLocalHostName\SqlExpress;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;"/>
</connectionStrings>


You need to find the authentication section again and add a new section underneath called <membership>.  Here you change the default provider to your providers name (just give it any name).  Then you need to add another section under that called <providers>.  Here you name your provider (call it the same name as the name you gave to default provider above).  There are other properties that you can change here is an example:

ASP.NET SAMPLE:


<connectionStrings>
        <add name="YourConnectionStringName" connectionString="Data Source=YourLocalHostName\SqlExpress;Initial Catalog=YourDataBaseName;Integrated Security=SSPI;"/>
</connectionStrings>


<system.web>
      <authentication mode="Forms" />
      <membership defaultProvider="YourProviderName">
        <!—Add this section to use your database instead-->
        <providers>
          <clear />
          <add
            name="YourProviderName"
            type="System.Web.Security.SqlMembershipProvider"
            connectionStringName="YourConnectionStringName"
            applicationName="YourApplicationName "
            enablePasswordRetrieval="false"
            enablePasswordReset="true"
            requiresQuestionAndAnswer="true"
            requiresUniqueEmail="true"
            passwordFormat="Hashed" />
        </providers>
      </membership>


Now we need to add a new user in order to test that we are using the correct database this time.
Open the ASP.NET Configuration for your site.  Once this opens click on the security tab and select “Create User”.  Fill in the textboxes – be sure to write down the passwords and usernames of your new user.

Back in Visual Studio test your controls again by running your site.  The logins should all work correctly.

Go back into SQL, make sure you select your database from the list and run your query again: SELECT * FROM aspnet_User.  This time you should get one line returned from your aspnet_User table containing the user name that you entered last.

If you do then you have correctly integrated and configured the SQLMembershipProvider with your existing Database!

0 comments: