我正在使用基于 Azure AD 角色的身份验证,我添加了 2 个角色(观察者、读者),这些角色分配给特定用户,工作正常。 list 文件中有这两个新条目。所有身份验证都正常工作,但我无法让 User.IsInRole() 返回 true,总是返回 false
我已将以下代码添加到 Startup.cs
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = SettingsHelper.ClientId,
Authority = String.Format(CultureInfo.InvariantCulture,SettingsHelper.AADInstance, SettingsHelper.TenantId),
PostLogoutRedirectUri = SettingsHelper.PostLogoutRedirectUri,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
// map the claimsPrincipal's roles to the roles claim
RoleClaimType = "roles",
},
}
当我查询 User.IsInRole("Observer") 时,它返回 false。我可以在用户的调试 session 中看到该声明
{ http://schemas.microsoft.com/ws/2008/06/identity/claims/role :观察者}
但无法访问它,这是一个常见问题吗?我做错了什么吗?
这是我的用户 session 变量
最佳答案
{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Observer}
您是如何添加自定义角色的?以下是我添加自定义角色的步骤,供您引用:
- 在 Azure AD 上注册应用
- 修改其 list 以添加自定义角色,如下所示:
"appRoles": [
{
"allowedMemberTypes": [
"User"
],
"displayName": "Orders",
"id": "51e10148-16a8-432a-b86d-ef620c3e48ed",
"isEnabled": true,
"description": "Oders can rise a order request",
"value": "Orders"
},
{
"allowedMemberTypes": [
"User"
],
"displayName": "Admin",
"id": "51e10148-16a8-432a-b86d-ef620c3e48ec",
"isEnabled": true,
"description": "Admins can manage roles and perform all task actions.",
"value": "Admin"
}
],
- 通过门户向用户分配角色
- 使用原始帖子中的代码将 Web 应用与 Azure 集成
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
RedirectUri = postLogoutRedirectUri,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
{
// map the claimsPrincipal's roles to the roles claim
RoleClaimType = "roles",
},
});
然后我们就可以得到如下图的角色:
关于asp.net-mvc - Azure AD 基于角色的身份验证,User.IsInRole(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41162255/