Back to: ASP.NET MVC Tutorial For Beginners and Professionals
Roles Management in ASP.NET Identity
In this article, I am going to discuss Roles Management in ASP.NET Identity. Please read our previous article, where we discussed Authorization in ASP.NET Identity.
Roles Management in ASP.NET Identity
In applications, roles are groups of users such as Admin, Developer, and Manager that control access to certain features. As explained in the ASP.NET Identity Architecture section, the Role manager handles all operations related to roles. I will guide you in configuring and utilizing the role manager in this article.
Creating ApplicationRoleManager Class:
Let us first create a Custom Role Manager Class. To do so, go to App_Start => IdentityConfig.cs class and create a new class with the name ApplicationRoleManager as follows:
// Configure the application role manager which is used in this application. public class ApplicationRoleManager : RoleManager<IdentityRole> { public ApplicationRoleManager(IRoleStore<IdentityRole, string> store) : base(store) { } public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context) { var manager = new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>())); return manager; } }
The ApplicationRoleManager class extends the generic RoleManager class that uses the IdentityRole as its type argument. It has a constructor that calls the base constructor of the RoleManager class and passes an IRoleStore<IdentityRole, string> object as an argument. Additionally, the class includes a static method called Create, which generates and returns a new instance of the ApplicationRoleManager class.
In order to use the ApplicationRoleManager class in our application, go to App_Start > Startup.Auth.cs class file and then add the following line of code inside the ConfigureAuth method:
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
The code above registers the ApplicationRoleManager and creates a callback function that will be used to create and store an instance of ApplicationRoleManager in the OwinContext. This will make it possible to retrieve the instance when needed. So, with the above statement, your ConfigureAuth method Startup.Auth.cs class should look as shown below.
How to Use ApplicationRoleManager?
We can use the created application role manager by calling the Get method on OwinContext. For a better understanding, please one controller with the name RollController and then copy and paste the following code into it.
using Microsoft.AspNet.Identity.Owin; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace AspNetIdentityWithNewProject.Controllers { public class RollController : Controller { private ApplicationRoleManager _roleManager; public ApplicationRoleManager RoleManager { get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); } private set { _roleManager = value; } } } }
In the code above, we have constructed a role controller that includes a property for managing application roles. To obtain the ApplicationRoleManager object, we use the Get method of the OWIN context and store it in the role manager property.
Namespace:
In order to use the Get method, you have to include the following namespace:
using Microsoft.AspNet.Identity.Owin;
Now, you can use the role manager property in your controller to perform role-related operations as follows:
In the next article, I am going to discuss How to Add, Update, and Delete Roles in ASP.NET Identity with Examples. Here, in this article, I try to explain Roles Management in ASP.NET Identity with Examples. I hope you enjoy this Roles Management in ASP.NET Identity article.