【Azure API 管理】解決API Management添加AAD Group時遇見的 Failed to query Azure Active Directory graph due to error 錯誤

問題描述

為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呢?

 

問題分析

查閱官方文檔(//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 的文檔後(//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的地址為://microsoftgraph.chinacloudapi.cn,需要進行替換
az rest --method PATCH --uri "//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的終結點。參考開發說明文檔://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 授權開發人員帳戶 //docs.azure.cn/zh-cn/api-management/api-management-howto-aad 。

 

動畫展示結果

 

 

參考資料

在 Azure API 管理中使用 Azure Active Directory 授權開發人員帳戶 ://docs.azure.cn/zh-cn/api-management/api-management-howto-aad

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

中國區 Azure 開發人員指南 ://docs.azure.cn/zh-cn/articles/guidance/developerdifferences