How do I get the names of the IdentityUser names in the view?

Using an ASP.NET MVC5 user ID, I am trying to display the IdentityUser role names in the view.

It seems that the user Identity has a Roles property that is a set IdentityUserRole

instead of IdentityRole

. The difference IdentityUserRole

is the relationship between IdentityUser

and IdentityRole

, and the IdentityRole

actual model [+] . Apparently there is no navigation between IdentityUser and IdentityRole. So all we get is the RoleId from .Role

IdentityUserRole

  • Is there an easy way to get the names of the roles?
  • Using the Fluent API, we can define a virtual navigation property between IdentityRole

    and IdentityUserRole

    so that we can easily get the name across the whole project?
  • If it's by design, what's the advantage of not letting us get IdentityRole

    straight?
+3


source to share


1 answer


You can select user id in id using role manager

var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this.dbcontext));

      



get userrole from user and then select name from this role

roleManager.FindById(user.RoleId).Name

      

-1


source







All Articles