【Azure API 管理】解决API Management添加AAD Group时遇见的 Failed to query Azure Active Directory graph due to error 错误

2023-04-25,,

问题描述

为APIM添加AAD Group时候,等待很长很长的时间,结果添加失败。错误消息为:

Write Groups ValidationError :Failed to query Azure Active Directory graph due to error: An error occurred while processing this request.

有什么办法可以成功为APIM添加AAD Group呢?

问题分析

查阅官方文档(https://docs.azure.cn/zh-cn/api-management/api-management-howto-aad),在为APIM配置Indenties(标识, 特指AAD中的注册应用)时,必须为注册应用赋予正确的API Permission.

1) Microsoft Graph的Directory.Read.All 权限

2) Azure Active Directory Graph的Directory.Read.All权限。

但是,在Azure AAD的页面中,现在无法找到Azure Active Directory Graph部分,无法通过Azure门户进行添加。

在深入分析 Global Azure 的文档后(https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-aad#add-an-external-azure-ad-group),得出可以通过PowerShell脚本来添加 Azure Active Directory Graph的Directory.Read.All权限。

PowerShell 脚本为:

$subId = "Your Azure subscription ID" #e.g. "1fb8fadf-03a3-4253-8993-65391f432d3a"
$tenantId = "Your Azure AD Tenant or Organization ID" #e.g. 0e054eb4-e5d0-43b8-ba1e-d7b5156f6da8"
$appObjectID = "Application Object ID that has been registered in AAD" #e.g. "2215b54a-df84-453f-b4db-ae079c0d2619"
#Login and Set the Subscription
az login
az account set --subscription $subId
#Assign the following permissions: Microsoft Graph Delegated Permission: User.Read, Microsoft Graph Application Permission: Directory.ReadAll, Azure Active Directory Graph Application Permission: Directory.ReadAll (legacy)
# 中国区graph的地址为:https://microsoftgraph.chinacloudapi.cn,需要进行替换
az rest --method PATCH --uri "https://microsoftgraph.chinacloudapi.cn/v1.0/$($tenantId)/applications/$($appObjectID)" --body "{'requiredResourceAccess':[{'resourceAccess': [{'id': 'e1fe6dd8-ba31-4d61-89e7-88639da4683d','type': 'Scope'},{'id': '7ab1d382-f21e-4acd-a863-ba3e13f7da61','type': 'Role'}],'resourceAppId': '00000003-0000-0000-c000-000000000000'},{'resourceAccess': [{'id': '5778995a-e1bf-45b8-affa-663a9f3f4d04','type': 'Role'}], 'resourceAppId': '00000002-0000-0000-c000-000000000000'}]}"

在以上的脚本中,必须注意以下几点:

1)  appObjectID 的值获取的不是注册应用的Application Id,而是注册应用的Object ID

2)  发送PATCH请求的终结点需要修改为中国区Azure的终结点。参考开发说明文档:https://docs.azure.cn/zh-cn/articles/guidance/developerdifferences

从 graph.microsoft.com 修改为  microsoftgraph.chinacloudapi.cn

3)  PATCH请求Body中包含的信息完全不用修改,它代表着Azure Active Directory Graph应用的Directory.ReadAll权限。

代码执行成功后,进入到Azure AD页面,查看是否已经存在Directory ReadAll权限

执行PowerShell命令

查看Directory ReadAll权限(注意:需要退出当前登录用户后,重新登录一次Azure 门户才可以看见)

最后,根据文档步骤,执行 Grant admin consent for {tenantname} 。

回到API Management页面,再次添加AAD Group。成功!

在完成这一步操作后,完全参考文档就可以实现: Azure API 管理中使用 Azure Active Directory 授权开发人员帐户 https://docs.azure.cn/zh-cn/api-management/api-management-howto-aad 。

动画展示结果

参考资料

在 Azure API 管理中使用 Azure Active Directory 授权开发人员帐户 :https://docs.azure.cn/zh-cn/api-management/api-management-howto-aad

Add an external Azure AD group : https://docs.microsoft.com/en-us/azure/api-management/api-management-howto-aad#add-an-external-azure-ad-group

中国区 Azure 开发人员指南 :https://docs.azure.cn/zh-cn/articles/guidance/developerdifferences

【Azure API 管理】解决API Management添加AAD Group时遇见的 Failed to query Azure Active Directory graph due to error 错误的相关教程结束。

《【Azure API 管理】解决API Management添加AAD Group时遇见的 Failed to query Azure Active Directory graph due to error 错误.doc》

下载本文的Word格式文档,以方便收藏与打印。