c# - Web Api 的 Azure AD 应用程序角色

标签 c# asp.net azure active-directory asp.net-web-api2

我已使用 Azure AD 保护 Web api 2 项目。使用 [Authorize] 属性效果非常好,但我无法让它与角色一起使用。我已将以下内容添加到应用程序 list 中:

"appRoles": [
{
  "allowedMemberTypes": [
    "Application"
  ],
  "description": "Access to example controller",
  "displayName": "Example",
  "id": "76d4bbb5-24c1-45ad-9446-cc360d7bd012",
  "isEnabled": "true",
  "origin": "Application",
  "value": "example"
}

]

并将此属性添加到我的 api Controller :[Authorize(Roles="example")]。如果我删除此属性并调用我的 api,则此声明将包含在 ClaimsPrincipal.Current 中:

{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: example}

但这不足以让我的应用程序在使用角色 header 时进行身份验证。任何帮助将不胜感激。

最佳答案

事实证明该 token 是 SAML token ,而不是 JWT。因此,为了使其正常工作,我只需要在 TokenValidationParameters 中设置它

TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters()
                {
                    // we inject our own multitenant validation logic
                    ValidateIssuer = false,
                    // map the claimsPrincipal's roles to the roles claim
                    RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role",

                }

关于c# - Web Api 的 Azure AD 应用程序角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35344348/

相关文章:

c# - 命名互斥体的单声道替代品

c# - 使用 EPPlus 将文本添加到 Excel 电子表格中的合并单元格

c# - 从俄罗斯网站读取 XML 时的编码问题

c# - VSTS - 在部署之前使应用程序离线

c# - 使用数据注释检查特定值

c# - 从 http 地址读取 XML 文件

c# - 如何从 PowerShell 调用 C# 类的索引器?

c# - 检测是否在 ASP.Net 中的移动浏览器上启用了 JavaScript

c# - 更新 Azure 表存储中的 RowKey 或 PartitionKey

Azure API管理: What is a good practise to handle different environments?