azure - 如何使用 Azure Ad client_credentials 对服务器到服务器通信的后端 Web api 进行身份验证

标签 azure curl asp.net-web-api oauth azure-active-directory

我有一个curl 脚本,它从Azure AD 请求新的访问 token 。我需要它为我的 API 返回某种声明,角色或范围都可以。

我可以获得访问 token ,但它不会返回角色声明或范围或任何类似性质的内容。

在 Web API 中使用时

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)                 
   .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd"));

我得到:

Neither scope or roles claim was found in the bearer token.

我确实尝试过设置

"AllowWebApiToBeAuthorizedByACL" : true,

appsettings.json但它并没有阻止错误的发生,正如它应该做的那样。

这用于在curl脚本和微服务之间进行授权。由于它是后台服务,因此没有用户交互,它只是服务器到服务器的授权。我只是想保护 Web api。因此,出于安全原因,我想确保创建的访问 token 实际上是该系统的访问 token ,这就是为什么我在我的应用程序上寻找某种声明或角色,以确保它不仅仅是来自另一部分的随机访问 token 系统的。

如果没有角色或范围,我的应用程序将引发以下错误

System.UnauthorizedAccessException: IDW10201: Neither scope or roles claim was found in the bearer token.

Oauth2 使用范围来使用户能够授予客户端对其数据的访问权限,因为这不需要我认为我正在寻找的不是范围。我认为我需要的是一种为应用程序本身定义角色的方法。我对 azure 的经验很少,所以如果我错了,请纠正我。

curl 脚本

curl -X POST -d "grant_type=client_credentials&client_id=api://a4994a31-c494-4b73-9622-d3a7144abeee&client_secret=[secret]&resource=api://a4994a31-c494-4b73-9622-d3a7144abeee" https://login.microsoftonline.com/1e2ad6d6-274f-43e8-89ef-d36d65bb83b5/oauth2/token

响应

    {
      "aud": "api://a4994a31-c494-4b73-9622-d3a7144abee",
      "iss": "https://sts.windows.net/1e2ad6d6-274f-43e8-89ef-d36d6583b5/",
      "iat": 1628506854,
      "nbf": 1628506854,
      "exp": 1628510754,
      "aio": "E2ZgYChZ8/PRc/eJ5sYfulfVXWUrBQA=",
      "appid": "a4994a31-c494-4b73-9622-d3a7144abeee",
      "appidacr": "1",
      "idp": "https://sts.windows.net/1e2ad6d6-274f-43e8-89ef-d36d65bb835/",
      "oid": "81f74198-ab14-4b0b-bad6-62893e15f9c8",
      "rh": "0.AQsA1tYqHk8n6EOJ79NtZbuDtTFKmaSUxHNLliLTpxRKvu4AAA.",
      "sub": "81f74198-ab14-4b0b-bad6-62893e15f9c",
      "tid": "1e2ad6d6-274f-43e8-89ef-d36d65bb83b",
      "uti": "FaZGzuuRn0eEQYPRReoYAQ",
      "ver": "1.0"
    }

我尝试设置范围

enter image description here

然后将其添加到curl脚本中

curl -X POST -d "grant_type=client_credentials&client_id=api://a4994a31-c494-4b73-9622-d3a7144abeee&client_secret=[Secret]&resource=api://a4994a31-c494-4b73-9622-d3a7144abeee&scope=api://a4994a31-c494-4b73-9622-d3a7144abeee/load_scope" https://login.microsoftonline.com/1e2ad6d6-274f-43e8-89ef-d36d65bb83b5/oauth2/token

完全没有效果,访问 token 仍然不包含任何范围。

然后我尝试添加角色

enter image description here

添加将角色添加到资源

curl -X POST -d "grant_type=client_credentials&client_id=api://a4994a31-c494-4b73-9622-d3a7144abeee&client_secret=[secret]&resource=api://a4994a31-c494-4b73-9622-d3a7144abeee/load" https://login.microsoftonline.com/1e2ad6d6-274f-43e8-89ef-d36d65bb83b5/oauth2/token

但这只是返回一个错误

{"error":"invalid_resource","error_description":"AADSTS500011: The resource principal named api://a4994a31-c494-4b73-9622-d3a7144abeee/load was not found in the tenant named 1e2ad6d6-274f-43e8-89ef-d36d65bb83b5. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.

list

我目前正在关注Assign app roles to applications这是 list 。

"appId": "51bdea30-a886-47f7-a27f-994c8caca5c",
    "appRoles": [
        {
            "allowedMemberTypes": [
                "Application"
            ],
            "description": "myapprole",
            "displayName": "myapprole",
            "id": "bdaa8bc7-e0b3-47b1-b92-9011313e16ae",
            "isEnabled": true,
            "lang": null,
            "origin": "Application",
            "value": "myapprole"
        }
    ],

最佳答案

解决方案是添加AllowWebApiToBeAuthorizedByACL,这会删除错误消息并允许后端服务无需角色或声明即可进行通信。

System.UnauthorizedAccessException: IDW10201: Neither scope or roles claim was found in the bearer token.

我已经添加了它,但在深入研究 Microsoft.Identity.Web 的源代码后它不起作用。我发现我使用的是旧版本 Microsoft.Identity.Web这显然是在添加 AllowWebApiToBeAuthorizedByACL 检查之前。我删除了 NuGet 包并在 1.15.2 中读取它并且它可以工作。

 "AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "[Redacted]",
    "ClientId": "[Redacted]",
    "TenantId": "[Redacted]",
    "AllowWebApiToBeAuthorizedByACL" : true,
    "CallbackPath": "/signin-oidc"
  },

关于azure - 如何使用 Azure Ad client_credentials 对服务器到服务器通信的后端 Web api 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68711323/

相关文章:

c# - 如何停止 .net JSON 序列化引用以前的元素?

azure - 具有启用专用端点的 Web 应用程序的应用程序网关配置

php - 如何处理 CURL 中服务器重置的连接

java - 有任何命令行实用程序(如 wget、curl 等)和/或 java 方法来获取缩短的 URL 的元数据?

ruby-on-rails - 将 curl 转换为等效的 ruby

.net - 二元运算符 LessThan 没有为类型 'System.Nullable` 1[System.DateTime ]' and ' System.Nullable`1[System.DateTimeOffset]' 定义

asp.net - 使用 Swashbuckle 记录模型属性

c# - Windows Azure 存储的默认顺序是什么?

azure - 批量查询异步复制 Blob Azure API 的状态

azure - 将消费计划上的 Azure Function App 设置为 64 位