How to Configure ASP.NET Membership using SQLMembershipProvider

In this article, we will learn to configure ASP.NET Membership using SQLMembershipProvider

Introduction

Objective of this article is to configure ASP.NET Membership to store user information in an external SQL Server database. We will use SQLMembershipProvider for this task.

ASP.NET Membership

ASP.NET Membership enables us to validate and store user information. It creates all the necessary tables and stored procedures to store and manage user credentials. It provides an API that can be used to manage user’s information programmatically.

ASP.NET Membership provides two types of Membership providers:

  1. SQLMembershipProvider: It is used to store user information in a SQL Server database
  2. ActiveDirectoryMembershipProvider: It is used to store user information in an Active Directory.

To configure SQLMembershipProvider first you need to configure your database.

Open Visual Studio Command Prompt (All Programs -> Microsoft Visual Studio 2008 -> Visual Studio Tools -> Visual Studio 2008 Command Prompt)

Type command: aspnet_regsql and press Enter

This will launch ASP.NET SQL Server Setup Wizard

Click Next

Leave default selection “Configure SQL Server for application services” and click Next again

Enter server name, Select SQL Server authentication and enter user id and password as in below figure. Select the database you want to configure. Here, I am using Employee database.

Click Next

Click Next and then Finish.

Now, you can see the new tables created in the Employee database for storing user credentials. These table names start with “aspnet_”. Similarly, you can also see new Stored Procedures created in the database with “aspnet_” prefix.

Now, we have configured our Employee database. Our next task is to enable SQLMembershipProvider in a Web application. Follow these steps to do that:

  • Create a new ASP.NET Web Application
  • Add <authentication mode=”Forms”/> in the Web.Config file. ASP.NET Membership works with only Forms Authentication
  • Add following to enable SQLMembershipProvider:
&lt;membership defaultProvider=&quot;MyMembershipProvider&quot;&gt;
     &lt;providers&gt;
          &lt;add name=&quot;MyMembershipProvider&quot;
               type=&quot;System.Web.Security.SqlMembershipProvider&quot;
               connectionStringName=&quot;ConString&quot; /&gt;
     &lt;/providers&gt;
&lt;/membership&gt;
  • Add connection string with name “ConString” to connect with Employee database
&lt;connectionStrings&gt;
     &lt;add name=&quot;SqlConString&quot;
          connectionString=&quot;Data Source=localhostSQL2005EXPRESS; User ID=youruserid; Password=yourpassword; Initial Catalog=Employee;&quot; /&gt;
&lt;/connectionStrings&gt;

Now, we are ready to use ASP.NET Membership to create new user using CreateUserWizard control and authenticate users using Login control.

6 thoughts on “How to Configure ASP.NET Membership using SQLMembershipProvider

  1. thirureka

    hi,
    Deepak when i select the server and database after that when i click next I am geetting error like this,

    ” Setup failed.

    Exception:
    An error occurred during the execution of the SQL file ‘InstallCommon.sql’. The SQL error number is 8152 and the SqlException message is: String or binary data would be truncated.

    pls help

    Reply
    1. DEEPAK SHARMA

      Hi,
      We have not set up any mail in this article. We have only configured ASP.NET membership in SQL Server database. Are you referring any other article? Please explain so that I can help you.

      Reply
  2. yahoo

    No matter if some one searches for his essential thing, so he/she wishes to be available that in detail, thus that thing is maintained over here.

    Reply
  3. Anees Ahmad

    dear sir, i am new in asp.net and i need complete help from creating “CreateUserWizard” to saving data into Database. please share with me overall code.
    thanks for given us such nice bloge

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *