为什么角色声明的类型不正确? [英] Why does the role claim have incorrect type?
问题描述
我下载了WebApp-RoleClaims-DotNet示例应用程序,但由于某种原因无法使其正常工作.
I downloaded the WebApp-RoleClaims-DotNet sample application and somehow cannot get it to work.
关于"页面包含以下相当简单的代码:
The about page has the following rather straightforward piece of code:
ClaimsIdentity claimsId = ClaimsPrincipal.Current.Identity as ClaimsIdentity;
var appRoles = new List<String>();
foreach (Claim claim in ClaimsPrincipal.Current.FindAll(claimsId.RoleClaimType))
appRoles.Add(claim.Value);
ViewData["appRoles"] = appRoles;
return View();
现在,即使我分配了适当的角色,表达式ClaimsPrincipal.Current.FindAll(claimsId.RoleClaimType)
也会返回一个空列表.
Now, the expression ClaimsPrincipal.Current.FindAll(claimsId.RoleClaimType)
returns an empty list, even though I have proper roles assigned.
我猜这是因为,根据调试器,角色声明的类型为roles
,而不是正确的名称空间http://schemas.microsoft.com/ws/2008/06/identity/claims/role
.
This is I guess, because -- according to the debugger -- the role claim have the type roles
instead of the proper namespace http://schemas.microsoft.com/ws/2008/06/identity/claims/role
.
我想念什么?
推荐答案
您能做到这一点吗?
我发现这可行:
ClaimsIdentity id = ClaimsPrincipal.Current.Identity as ClaimsIdentity;
var appRoles = new List<string>();
foreach (Claim claim in ClaimsPrincipal.Current.Claims)
{
appRoles.Add(claim.Value);
}
我希望它可以帮助遇到类似问题的人.
I hope it helps someone with a similar problem.
这篇关于为什么角色声明的类型不正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!