Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3: How to get User's Role in a Controller Method?

I want to be able to

  1. Get a list of roles of the current authenticated user.
  2. Filter the data provided to that user based on their role.

I see ways to check if the user is in a particular role, but I don't care what role they participate. The database will know what roles are allowed to see what data. I need to submit a collection of these roles to the data service to filter the data based on those roles.

So first step is how do I get all roles associated with the current user in a controller method?

EDIT:

This seemed to work out nicely:

Roles.GetRolesForUser(User.Identity.Name)

Supporting answers still welcome.

like image 703
Zachary Scott Avatar asked May 20 '11 03:05

Zachary Scott


Video Answer


2 Answers

Roles.GetRolesForUser(User.Identity.Name)
like image 58
Zachary Scott Avatar answered Sep 17 '22 19:09

Zachary Scott


This can be done with a single statement:

User.IsInRole("admin");
like image 21
Doctor Sid Avatar answered Sep 21 '22 19:09

Doctor Sid