天天看點

【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.

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

有什麼辦法可以成功為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門戶進行添加。

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

在深入分析 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權限。

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

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指令

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

檢視Directory ReadAll權限(注意:需要退出目前登入使用者後,重新登入一次Azure 門戶才可以看見)

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

最後,根據文檔步驟,執行 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 管理】解決API Management添加AAD Group時遇見的 Failed to query Azure Active Directory graph due to error 錯誤

參考資料

在 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

當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!

繼續閱讀