从函数应用访问 Azure Service Manager API

问题描述 投票:0回答:1

我需要从函数应用程序访问Azure Service Manager API。如何使用应用程序注册进行身份验证?

我已经创建了一个应用程序注册,并将其委托给Azure Service Manager(在azure门户中的API权限选项卡上)。但是,当我获取访问令牌并尝试进行 api 调用时,我收到 403 响应:

对象 id x 的客户端 x 无权执行 行动。

是因为权限被委托了吗? Azure Service Manager 的应用程序权限似乎不可用。

编辑:

我要发送的查询:

https://management.azure.com/subscriptions/<subscription id>/resourceGroups/<resource group>/providers/Microsoft.Web/sites/<service name>/restart?api-version=2022-03-01

我通过发送post请求来获取身份验证令牌:

URL: https://login.microsoftonline.com/<tenant id>/oauth2/v2.0/token
Payload: grant_type=client_credentials&scope=https://management.azure.com/.default&client_id=<client id>&client_secret=<client secret>
azure azure-active-directory
1个回答
0
投票

要访问 Azure 服务管理, 创建 Microsoft Entra ID 应用程序并授予 API 权限,如下所示:

enter image description here

使用如下参数生成访问令牌

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSceret
grant_type:client_credentials
scope:https://management.azure.com/.default

enter image description here

当我尝试访问 Azure 服务管理器时,API 调用失败并出现

403
错误:

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/restart?api-version=2023-12-01

enter image description here

要解决该错误,您需要向订阅级别下的服务主体授予所需的 RBAC 角色

在此场景中,将 贡献者角色网站贡献者角色 分配给服务主体:

转到 Azure 门户 -> 订阅 -> 选择您的订阅 -> 访问控制 (IAM) -> 添加角色分配 -> 选择角色 -> 选择成员 -> 选择您的服务主体 -> 查看+分配

enter image description here

现在再次生成访问令牌,现在我可以成功重新启动网络应用程序了

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/restart?api-version=2023-12-01

enter image description here

确保为 Microsoft Entra ID 应用程序创建服务主体:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.