I created a role based menu for which I followed this tutorial. Some where down that page you'll see this line of code:
String[] roles = Roles.GetRolesForUser();
It returns all roles of the currently logged in user. I was wondering how to accomplish this with the new ASP.NET Identity system?
It's still pretty new and there is not much to find about it.
You can use Roles. GetRolesForUser() method to get all the rols user belong to . use it like this; string[] rolesuserbelongto = Roles.
The following code example uses the GetUsersInRole method to get a list of the users in a particular role and binds the results to a GridView control.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With