Add Property to ASP.NET Identity Role

In previous post, I showed you how to add property to Identity user. Another common requirement when using ASP.NET Identity is adding new property to IdentityRole. In this post I’ll share a step by step guide which is inspired by my answer to this question question in stackoverflow about adding new property to Identity role.

To add a new property to IdentityRole, you can follow the following steps:

  1. Create an ASP.NET Web Application
  2. Make sure you select MVC and the Authentication is Individual User Accounts
  3. Go to Models folder → Open IdentityModels.cs and Create ApplicationRole class containing the custom property that you want to add:
    public class ApplicationRole : IdentityRole   
    {
        public string Description { get; set; } 
    }
    
  4. Change GenerateUserIdentityAsync method of ApplicationUser to accept parameter of type of UserManager<ApplicationUser, string>:
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, string> manager)
        {
    
  5. Change ApplicationDbContext base class and introduce all the generic parameters:
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
    
  6. Build the project.

  7. Go to TOOLS menu → Nuget Package Manager → click Package Manager Console

  8. Type Enable-Migrations and press Enter and wait until the task get completed.

  9. Type Add-Migration "ApplicationRole" and press Enter and wait until the task get completed.
  10. Type Update-Database and press Enter and wait until the task get completed.
  11. Go to App_Start folder → Open IdentityConfig.cs and Change the ApplicationUserManager class to derive from UserManager<ApplicationUser, string> and also change its Create method to return a UserManage aware of ApplicationRole:

    public class ApplicationUserManager : UserManager<ApplicationUser, string>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
            : base(store)
        {
        }
    
        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>(context.Get<ApplicationDbContext>()));
    
  12. To manage roles, create ApplicationRoleManager class in the same file:
    public class ApplicationRoleManager : RoleManager<ApplicationRole>
    {
        public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store) : base(store) { }
    
        public static ApplicationRoleManager Create(
            IdentityFactoryOptions<ApplicationRoleManager> options,
            IOwinContext context)
        {
            return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApplicationDbContext>()));
        }
    }
    
  13. Go to App_Start folder → Open Startup.Auth.cs and add the following code to the ConfigureAuth method:
    ConfigureAuthapp.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    

Now the project is ready to take advantage of the new ApplicationRole.

You May Also Like

About the Author: Reza Aghaei

I’ve been a .NET developer since 2004. During these years, as a developer, technical lead and architect, I’ve helped organizations and development teams in design and development of different kind of applications including LOB applications, Web and Windows application frameworks and RAD tools. As a teacher and mentor, I’ve trained tens of developers in C#, ASP.NET MVC and Windows Forms. As an interviewer I’ve helped organizations to assess and hire tens of qualified developers. I really enjoy learning new things, problem solving, knowledge sharing and helping other developers. I'm usually active in .NET related tags in stackoverflow to answer community questions. I also share technical blog posts in my blog as well as sharing sample codes in GitHub.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *