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.
Prerequisites
- Visual Studio 2015
- SQL Server 2008 and above
Developed
- Developed in ASP.NET MVC 5
Grids Used
- DataTables Grid
- jTable
NuGet Packages
- CaptchaMvc
- AutoMapper
- MVC
- EntityFramework 6.0
- Dapper
Creating Application
We are going to create an application with the name “ManageRoles”.
After creating the project, next, we are going to add a class library for Model, Repository, ViewModels.
After adding the class library, we add the NuGet packages.
- CaptchaMvc
- AutoMapper
- MVC
- EntityFramework 6.0
- Dapper
After installing, let us have a look at the database.
Database
For this application, I have created a database with name “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.
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.
Next, we are going to have a look at the Controller folder.
Controllers Folder
This folder contains the 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.
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.
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.
AutoMapper
For Mapping objects, we have used an Automapper.
Automapper has a configuration class; in that class we are going to keep all mappings.
In AutoMapperConfiguration Class, we have kept all the mappings.
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
- Screen of Login
The common Login screen for SuperAdmin, Admin, Users.
After logging into Application, first, we are going to see the Dashboard with Side Menus.
- 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.
- 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.
After adding Menu, you can see all the Menus which you have added by clicking on the "Back to List" button.
Next, we are going to create a SubMenu.
- 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”.
After adding SubMenu, you can see all SubMenus which you have added by clicking on the "Back to List" button.
After creating the SubMenu, next, we are going to create a role.
- 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.
After adding Role, you can see all Roles by clicking on the "Back to List" button.
After creating a role, next, we are going to assign this role to the users.
- 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.
If the role is already assigned to User, then it will show you a message.
- 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.
After assigning a role to the menu, next, we are going to see all assigned roles to menus.
After assigning a role to the menu and showing all assigned menus, next, we are going to assign roles to submenus.
- 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
After assigning a role to Submenu, we are going see all assigned roles to SubMenus.
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.
- Activate, Deactivate and Delete Assigned Menu
In this part, we are going to see how to activate and deactivate the 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.
In a similar way, you can delete assigned menu role by clicking on delete icon.
- 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.
In a similar way, you can delete the assigned SubMenu role by clicking on delete icon.
- Creating Users
In this part, SuperAdmin has a right to create User and while creating User we can assign a role to it.
After creating Users, you can see all created Users.
- 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.
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.
After logging into the application, the below dashboard can be viewed with the only menu which SuperAdmin has assigned.
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.