2

I want to be able to extend the default implementation of IdentityRole to include fields like Description. It's easy enough to do this for IdentityUser because IdentityDbContext takes a generic parameter of type IdentityUser. However, IdentityDbContext doesn't allow you to do this for IdentityRole. How can I accomplish this?

I know I can create a basic DbContext, and implement my own IUserStore, so that I can use my own role class, but I really don't want to have to do that.

Any thoughts?

2 Answers 2

10

I have just gone through this pain myself. It actually turned out to be pretty simple. Just extend IdentityRole with your new properties.

public class ApplicationRole : IdentityRole { public ApplicationRole(string name) : base(name) { } public ApplicationRole() { } public string Description { get; set; } } 

Then you need to add the line

new public DbSet<ApplicationRole> Roles { get; set; } 

into your ApplicationDbContext class like this otherwise you will get errors.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") {} new public DbSet<ApplicationRole> Roles { get; set; } } 

thats all I needed to do. Make sure you change all instances of IdentityRole to ApplicationRole including anything you are seeding. Also, dont forget to issue a "update-database" to apply the changes to your DB. Any existing rows in there won't be seen by your new RoleManager unless you have the "ApplicationRole" set as a discriminator. You can easily add this yourself.

HTH

Erik

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this answer. I think I did everything except setting "ApplicationRole" as discriminator. I was just wondering why this was done... new public DbSet<ApplicationRole> Roles { get; set; }
2

UserManager<TUser> uses UserStore<TUser> as its user store (IUserStore). UserManager works with UserStore for adding and removing user to a role name as IUserRole.

Likewise, there are interfaces IRole & IRoleStore<TRole> for IdentityRole and RoleStore<TRole> where TRole is IdentityRole. This is to work plainly with Roles directly.

So you can inherit IdentityRole and add additional information. Use RoleStore<MyRole> to manage it along additional information.

RoleManager<TRole> provides core interaction methods for Role, which can use MyRoleStore.

MyIdentityRole.cs

public class MyIdentityRole: IdentityRole { public String Description { get; set;} } 

2 Comments

That's not what I am looking for though. I'm talking about the actual storage mechanism. IdentityDbContext allows you to use your own IdentityUser, but it forces the use of IdentityRole.
The above customization is same approach to IdentityUser except the store is RoleStore instead of UserStore. Both the store uses IdentityDbContext. IdentityDbContext, automatically takes those new fields in IdentityUser, but for IdentityRole you need to use OnModelCreating for providing Db mapping and constrains for new fields added to it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.