AutoCreate SimpleMembership Tables in ASP.Net MVC4

This article describes how to use SimpleMembership of WebMatrix.Authenication and authorization that is a very necessary part of web applications. SimpleMembership, introduced with WebMatrix, tries to address these issues by offering a flexible model for authenticating users.

This article explains how to integrate with an existing database and issues confronted during development.

Learn How

You can also have a look at MVC-related articles here:

Step 1: I created an internet application and I have already a built-in database in SQL Server 2008.

Sql Server 2008

Step 2: Select internet application from the project templates as shown in the following image:

project template

This is the default structure of the application as shown in the following image:

default structure of application

Step 3: I have an existing database where I want to integrate SimpleMemberShip as depicted in the following image:

SimpleMemberShip

I've created a table Users with a few columns in an existing database. Columns such as ID and UserName are important for us that I will be talking about later in this article.

Step 4: There are 4 files where I will be concentrating more during this article as depicted in the image below:

concentrating

Step 5: Open you web.config and put this connection string in the connectionString tag as depicted in the image below:

web config

And configure these lines just behind the Authentication mode tag to enable SimpleMembershipProvider and roleManager as shown in the following image:

SimpleMembershipProvider

Step 6: Now open AccountModel.cs and change the RegisterModel as our need to, I added a new property Email into model to meet my requirements as shown in the following image:

RegisterModel

Step 7: Open the AccountController class and you will see that it was already decorated with an Annotation InitializeSimpleMembership .This class executes first to AccountController and is responsible for the database creration if it doesn't exist and also creates simple membership tables by connecting to the database. Kindly see the following image:

AccountController

Step 8: We are done with all configuration setup, now run an application and click on the register button on the top-right as shown in the following image:

register button

As soon as you click on the register button it initializes the InitializeSimpleMembership class and does the procedure to create SimpleMembership. Kindly see the image below:

steps to create SimpleMembership

Step 9: Fill in the required details and register the user as shown in image below:

required details

Step 10: All the information related to the employee has been stored in the database in two tables as well as the user “dotnet” logged in user in the web site. Kindly see the image shown below:

user

In the next article I will tell you about some errors that I encountered during this development.

I hope you learned SimpleMemberShip.

Note: Friends I am not able to upload the sample application that I created since it exceeds the limit of 10 MB .This application is built using Visual Studio 2012.

Done

I hope you enjoyed and it may help you down the line.

Up Next
    Ebook Download
    View all
    Learn
    View all