asp.net identity get all roles of logged in user
Controller.User.Identity
is a ClaimsIdentity
. You can get a list of roles by inspecting the claims...
var roles = ((ClaimsIdentity)User.Identity).Claims
.Where(c => c.Type == ClaimTypes.Role)
.Select(c => c.Value);
--- update ---
Breaking it down a bit more...
using System.Security.Claims;
// ........
var userIdentity = (ClaimsIdentity)User.Identity;
var claims = userIdentity.Claims;
var roleClaimType = userIdentity.RoleClaimType;
var roles = claims.Where(c => c.Type == ClaimTypes.Role).ToList();
// or...
var roles = claims.Where(c => c.Type == roleClaimType).ToList();
Here's an extension method of the above solution.
public static List<string> Roles(this ClaimsIdentity identity)
{
return identity.Claims
.Where(c => c.Type == ClaimTypes.Role)
.Select(c => c.Value)
.ToList();
}