Creating Custom Role Manager In ASP.NET MVC 5

In this article, we are going to learn how to create and use custom role manager in ASP.NET MVC in a step-by-step way.

Role Management is the most required part of any application. Using role management, you can provide access (rights) to the user to access the application. The most common rights you might have heard of are SuperAdmin, Admin, and User. The top hierarchy is given to SuperAdmin; below that is Admin, and the last level is for Users.

SuperAdmin

Prerequisites

  1. Visual Studio 2015
  2. SQL Server 2008 and above

Developed

  1. Developed in ASP.NET MVC 5

Grids Used

  1. DataTables Grid
  2. jTable

NuGet Packages

  1. CaptchaMvc
  2. AutoMapper
  3. MVC
  4. EntityFramework 6.0
  5. Dapper

Creating Application

We are going to create an application with the name “ManageRoles”.

Creating Application

After creating the project, next, we are going to add a class library for Model, Repository, ViewModels.

ManageRoles

After adding the class library, we add the NuGet packages.

  1. CaptchaMvc
  2. AutoMapper
  3. MVC
  4. EntityFramework 6.0
  5. Dapper

    NuGet’s packages

After installing, let us have a look at the database.

Database

For this application, I have created a database with name “ManageRoleDB”.

ManageRoleDB

 

Next, we are going to have a look at the Dependency Injection.

Dependency Injection

We are using Unity Framework for Dependency Injection in that constructor injection.

When we install Unity Framework from NuGet package, it adds two classes in the App_Start folder.

App_Start folder

In that, we are going to use UnityConfig class for RegisterTypes. There might be a question of what we are going to do with this method.

In this method, we are going to keep a mapping of interface and its concrete class.

mapping of interface

Next, we are going to have a look at the Controller folder.

Controllers Folder

This folder contains the controllers.

Controllers

Next, we are going to have a look at “ManageRoles.Models” Class library.

ManageRoles.Models

In this class library, we have kept all the Models which are used in the application.

ManageRoles.Models

Next, we are going to have a look at “ManageRoles.ViewModels” Class library.

ManageRoles.ViewModels

In this class library, we have kept all the ViewModels which are used in the application.

ManageRoles.ViewModels

Next, we are going to have a look at “ManageRoles.Repository” Class library.

ManageRoles.Repository

In this class library, we have kept all Interfaces and Concrete classes which are used in the application.

ManageRoles.Repository

AutoMapper

For Mapping objects, we have used an Automapper.

Automapper has a configuration class; in that class we are going to keep all mappings.

AutoMapper

In AutoMapperConfiguration Class, we have kept all the mappings.

AutoMapperConfiguration Class

Next, after understanding the automapper, next, we are going to have a look at all application screens (in-short, how it functions).

First, we are going to see all the SuperAdmin screens.

Screen of SuperAdmin

  1. Screen of Login
    The common Login screen for SuperAdmin, Admin, Users.

    Screen of Login

    After logging into Application, first, we are going to see the Dashboard with Side Menus.
  1. SuperAdmin Dashboard

    SuperAdmin Dashboard

    After clicking on Masters menu, you will find all Menus related to Master that includes Create Menu, Submenu, and Roles, then Assign Role to User and many more menus.

    SuperAdmin Dashboard
  1. Create Menu
    In this part, we are going to create Main menus and for creating that, we are going to take controller name, action name, Menu name as input. Along with this, we have the status for Menu - active and deactivated. Another checkbox is for the cache. After choosing this option, we are going to cache all menus.

    Create Menu

    After adding Menu, you can see all the Menus which you have added by clicking on the "Back to List" button.

    Create Menu

    Next, we are going to create a SubMenu.
  1. Create SubMenu
    In this part, we are going to add a SubMenu. For doing that, we need to choose the Main Menu under which this submenu will be added. Next, we need to add ControllerName, ActionMethod, and SubMenuName. The SubMenuName will be shown to the users. And finally, the status to make this SubMenu activate or deactivated.

    Example - As you can see, “Master” is the main menu and under that, we are adding “Create SubMenu”.

    Create Submenu

    After adding SubMenu, you can see all SubMenus which you have added by clicking on the "Back to List" button.

    Create Submenu

    After creating the SubMenu, next, we are going to create a role.
  1. Create Role
    In this part, we are going to create a new role. For doing that, we just need to enter the name of the role.

    Example - SuperAdmin, Admin, User.

    Create Role

    After adding Role, you can see all Roles by clicking on the "Back to List" button.

    Create Role

    After creating a role, next, we are going to assign this role to the users.
  1. Assign Roles to Users
    In this part, we are going to assign roles to the users. The User dropdown list has a list of usernames and Role dropdown has a list of Roles which we have created.

    Just choose the username and role and click on the "Assign Role" button to assign a role to the user.

    Assign Roles to Users

    If the role is already assigned to User, then it will show you a message.

    Assign Roles to Users
  1. Assign Roles to Menu
    We have already created a menu but we have not assigned it. So far, in this part, we are going to assign the menu to a role such that the person assigned with that role can view that menu.

    Assign Roles to Menu

    After assigning a role to the menu, next, we are going to see all assigned roles to menus.

    Assign Roles to Menu

    After assigning a role to the menu and showing all assigned menus, next, we are going to assign roles to submenus.
  1. Assign Roles to SubMenu
    We have already created Submenu but we have not assigned it yet. In this part, we are going to assign SubMenu to a role such that the person assigned with that role can view that SubMenu

    Assign Roles to SubMenu

    After assigning a role to Submenu, we are going see all assigned roles to SubMenus.

    Assign Roles to SubMenu

    Now we have assigned a role to menu and sub Menu along with that we have also assigned a role to User. Next, we are going see how we can activate and deactivate the menu.
  1. Activate, Deactivate and Delete Assigned Menu
    In this part, we are going to see how to activate and deactivate the menu.

    Activate, Deactivate and Delete Assigned Menu

    To deactivate just click on Edit icon.

    It will pop up a dialog in that you can check and uncheck the checkbox for activating and deactivating menu.

    Activate, Deactivate and Delete Assigned Menu

    In a similar way, you can delete assigned menu role by clicking on delete icon.

    Activate, Deactivate and Delete Assigned Menu
  1. Activate, Deactivate and Delete Assigned SubMenu
    In this part, we are going to see how to activate and deactivate SubMenu. To deactivate just click on Edit icon.

    It will pop up a dialog in that you can check and uncheck the checkbox for activating and deactivating the SubMenu.

    Activate, Deactivate and Delete Assigned SubMenu

    In a similar way, you can delete the assigned SubMenu role by clicking on delete icon.

    Activate, Deactivate and Delete Assigned SubMenu
  1. Creating Users
    In this part, SuperAdmin has a right to create User and while creating User we can assign a role to it.

    Creating Users

    After creating Users, you can see all created Users.

    Creating Users
  1. Remove Assigned Role to Users
    In this part, we have an option where we can remove a role from the user such that we can assign a new role to User.

    Remove Assigned Role to Users

Screen of User - Login to Application with User Role

In this part, we are going to log in to the application with User Credentials to check what kind of Menu and SubMenu the User can View.

Login to Application with User Role

After logging into the application, the below dashboard can be viewed with the only menu which SuperAdmin has assigned.

SuperAdmin

Finally, we have learned how to develop Custom Role Manager in ASP.NET MVC 5 and along with that, we have also learned how to use Automapper, Unity.MVC packages, and Grids.

Download source code.


Similar Articles