简体   繁体   中英

How can I list the Roles the current user belongs to in Identity 2.0

I need to list the Roles that the user currently logged into my ASP.NET web forms site is a member of.

var userManager = new UserManager(new UserStore());

IdentityUserRole[] userRoles = userManager.FindByName(User.Identity.Name).Roles.ToArray();

var roles = ((ClaimsIdentity)User.Identity).Claims
                .Where(c => c.Type == ClaimTypes.Role)
                .Select(c => c.Value);

Should be something like this on the top of my head

add .ToList() if needed :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM