asp.net身份获取login用户的所有angular色
我创build了一个基于angular色的菜单,我按照这个教程。 在那个页面的一些地方你会看到这行代码:
String[] roles = Roles.GetRolesForUser();
它返回当前login用户的所有angular色。 我想知道如何用新的ASP.NET身份系统来实现这个function?
这还是很新的,没有什么可以find的。
Controller.User.Identity
是一个ClaimsIdentity
。 您可以通过检查索赔来获取angular色列表…
var roles = ((ClaimsIdentity)User.Identity).Claims .Where(c => c.Type == ClaimTypes.Role) .Select(c => c.Value);
—更新—
打破了一下更多…
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();
这是上述解决scheme的扩展方法。
public static List<string> Roles(this ClaimsIdentity identity) { return identity.Claims .Where(c => c.Type == ClaimTypes.Role) .Select(c => c.Value) .ToList(); }