Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp MVC: How get roles from ApplicationUser

In ASP.NET MVC 5, in a controller, I have take the user that has make the request with:

ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

With the ApplicationUser instance, how can i get all the Roles of the user?

like image 293
Tom Avatar asked Dec 19 '22 20:12

Tom


1 Answers

You can get user and assigned roles by using UserManager.

var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

and then you can get your user like you already did, and also you can get roles for particular user by calling GetRoles method

userManager.GetRoles(userId);
like image 66
IMujagic Avatar answered Jan 04 '23 23:01

IMujagic