rukiman
rukiman

Reputation: 643

Find out the user role in MVC

I am using the identity model that comes standard with the MVC template.

I can find the role the user is in using user.Roles which gives me a list of IdentityUserRole. But I only have access to the RoleId.

I basically what to check if the user is in the "Admin" role.

Upvotes: 1

Views: 1131

Answers (3)

kodikotriftis
kodikotriftis

Reputation: 166

You can use UserManager to check if an application user has some role. Try

bool result = _userManager.IsInRole("userId", "Admin");

Or

bool result = await _userManager.IsInRoleAsync("userId", "Admin");

Upvotes: 0

Prasad Joshi
Prasad Joshi

Reputation: 471

You can also use Enums for this purpose for more details regarding visit More Help regarding Enum

Upvotes: -1

Siby Thomas
Siby Thomas

Reputation: 81

please try this

if(User.IsInRole("Admin"))
{
  //Your code
}

Upvotes: 2

Related Questions