Create identity in simple way using ASP.NET MVC 5 Part Two (Role Based)
Introduction
First part of article explained about how to create identity in simple way. This article explains about how to create identity with “Roles” in simple way using ASP.NET MVC. Before read this article please read first part of this article. Link for first part of article.
Definition
Role based Identity is a security way of authentication with roles in web applications. It is identify the authorized user with roles.
Background
Identity is one of secured way to access in our applications. Identity is only not providing full security. instead of identity we are using identity with roles. A role is more secure one because it is restrict user access controls of application. Role is denoted, access permission to users, for example if you are an admin for web application you can do any modification in application but if you are an user you cannot do any modification, you access might be restricted.
Steps for Crating Identity with Roles
Step 1
Follow first part of this article’s steps, before creating Identity with roles. Link for first part of this article
Step 2
Go to solution explore, expand App_Data folder and double click “aspnet-NewIdentity-20160726112300.mdf” file. Now you can see all the tables which you have created using default identity.
Step 3
We want to store all roles in AspNetRoles table for using application. Right click on “AspNetRoles” table and click New Query.
Step 4
New Query window will be open after click new query options. We can write any SQL query in sql query window and run like below screen shorts.
Step 5
Insert roles in “AspNetRoles” table using SQL query. Below screen shorts explains how to insert roles in specified table.
Step 6
We need to map with role id and users id now. We already saved user details in “AspNetUsers” tables. Using “AspNetUserRoles” table we can map users and roles. We are mapping using “UserId” field from “AspNetUser” table and “RoleId” field from “AspNetUsers” table into “AspNetUserRoles” table.
Step 7
We can map many roles to users using previous step. These roles and users are working with the help of OWIN middleware.
I assigned my user id to admin role now we use role in coding part. Based on role we can access in our application. We need to write coding like below.
Coding For Roles
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace NewIdentity.Controllers
{
[Authorize]
public class TestController : Controller
{
// GET: Test
/// <summary>
/// Identity With Role only for Identity action method.
/// </summary>
/// <returns></returns>
///
[Authorize(Roles="Admin")] // Admin only can access
public ActionResult Identity()
{
return Content("We are using Identity");
}
public ActionResult NonIdentiy()
{
return Content("We are not using Identity");
}
}
}
Explanation
Here we assign admin role to “Identity” action methods so other user can not access “Identity” action method. That is meaning of “[Authorize(Roles="Admin")]”.
IF run application http://localhost:51868/Test/Identity URL, It will redirect to login page because we are using identity with admin role.
Finally enter our credential and enter specified page. We assigned “admin” Role to vicky1@gmail.com user as well as assign admin role to “Identity” action method.
If try to enter different user but same URL, we cannot enter because we do not assign admin role to any other user.
We are trying to login using test@gmail.com , it is success fully login but do not go specified page. It is again to login page because of roles. We can see below screen short.
We can assign roles in controller levels as well as action methods level. We can assign controller level following way.
Roles in Controller Level
namespace NewIdentity.Controllers
{
// GET: Test
/// <summary>
/// Identity With Role in controller level.
/// </summary>
/// <returns></returns>
///
[Authorize(Roles = "Admin")]
public class TestController : Controller
{
public ActionResult Identity()
{
return Content("We are using Identity");
}
public ActionResult NonIdentiy()
{
return Content("We are not using Identity");
}
}
}
We assign role for “Test” controller as “Admin”, so those who have admin role that persons only access in “Test” controller. Other role user can not access “Test” Controller.
Conclusion
This and this part of previous article explains about Identity and roles fully. It helps to those who are newly learning about identity and roles in ASP.NET MVC. Next part of article explains how to create custom identity and roles in ASP.NET MVC.