Membership and Authorization in ASP.Net MVC 3 Razor



In this article I will describe how to do membership and Authorization in an ASP.Net MVC 3 Application.

Description: One difference between the full ASP.NET MVC 3 Web Application template and the ASP.NET MVC 3 Empty Web Application template is that the empty template doesn't include an Account Controller. That means you have to do your entire customization login form authentication for user and role.

Please see my articles about customizing Login form authentication.

Article 1
Article 2
 
But if you want to do Membership and Authorization by system account then we have to use ASP.NET MVC 3 Web Application template.

Step 1:

Create a New ASP.NET MVC 3 project named "Membership" just like Figure 1. Click Ok.

MVC1.gif

Figure 1:

Step 2:

After Clicking "Ok" you will find a Window where you have to choose Project Type

  1. Empty
  2. Internet application

If you choose "Empty" project type then the project will not have by default code for membership roles setting.

If you choose "Internet application" then you will see by default the account controller class and model class created. And you will have all the code needed for membership and roles.

Select the "Internet application" project type and choose the default view engine Razor just like Figure 2:

MVC2.gif

Figure 2:

Step 3:

When you click Ok finally you will see the "membership" project having "accountcontroller" under the "Controller" folder and the "accountmodel" under the model folder. You will see that account login related Views are also created under the "views" folder Just like Figure 3.

This will be created automatically because we choose the project type "Internet" application.

MVC3.gif

Figure 3:

Step 4:

Before we require Authorization in our website, we'll need to create a user with access. The easiest way to create a user is to use the built-in ASP.NET Configureuration website.

Click the ASP.NET configuration website by clicking the icon in the Solution Explorer just like Figure 4 marked in red.

MVC4.gif

Figure 4:

This will launch a configuration website.

Step 5:

Click the Security tab on the home screen, after that click the "Enable roles" link in the center of the screen just like Figure 5 and Figure 6:

MVC5.gif

Figure 5:

Step 6:

Click the "Create or Manage roles" like Figure 7 marked in red.

MVC6.gif

Figure 6:

MVC7.gif

Figure 7

Step 7:

Now enter "Administrator" as the role name textbox and after that press the Add Role button.

Step 8:

Click the Back button then after that on the left side click on the Create user link. Just like Figure 8.

MVC8.gif

Figure 8:

Step 9:

After that fill in the user information fields on the left side box by giving your required credentials just like Figure 9.Check the administrator

MVC9.gif

Figure 9:

After clicking the "createuser" button you will see a message saying "Your account has been successfully created.".

Just like Figure 10:
MVC10.gif

Figure 10

Conclusion:

So in this article we have learned how to create the settings of membership in ASP.Net MVC 3 using ASP.Net web configuration setting.


Similar Articles