問題描述

在 21V(中國運營的 Microsoft 雲,世紀互聯)環境中,需要通過 Microsoft Graph API獲取某個用户的 accountEnabled(賬號啓用/禁用)狀態。

由於國家雲與全球版在 令牌頒發端點、Graph 服務根域名、以及 權限作用域(scope) 上存在差異,很多“全球版”教程在中國區直接套用會出現 401/403 或取不到該屬性(返回 null)的問題。

本文聚焦“在中國區Azure環境中,正確查詢指定用户的 accountEnabled 值”的可操作步驟

問題解答

第一步:連接中國區Azure環境並獲取Token

az cloud set --name AzureChinaCloud

az login

az account get-access-token --resource 'https://microsoftgraph.chinacloudapi.cn/'

【Azure Developer】中國區Azure環境中查看用户賬號是否可用(accountEnabled)的操作步驟_microsoft


第二步:使用發送REST API的客户端發送GET請求獲取用户的User ID

GET https://microsoftgraph.chinacloudapi.cn/v1.0/users?$filter=userPrincipalName eq 'your login user account , the format is xxx@xxx.xxx.onmschina.cn'

 

第一步中獲取的Token作為Authorization值,請求返回的格式如下:

{
  "@odata.context": "https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#users",
  "value": [
    {
      "businessPhones": [],
      "displayName": "your name",
      "givenName": null,
      "jobTitle": null,
      "mail": "xxx@xxx.xxx.onmschina.cn",
      "mobilePhone": null,
      "officeLocation": null,
      "preferredLanguage": null,
      "surname": null,
      "userPrincipalName": "xxx@xxx.xxx.onmschina.cn",
      "id": "xxx-xxx-xxx-xxx-xxx"
    }
  ]
}

 

第三步:獲取user的account狀態

GET https://microsoftgraph.chinacloudapi.cn/v1.0/users/<xxx-xxx-xxx-xxx-xxx>?$select=displayName,accountEnabled

第一步中獲取的Token作為Authorization值,第二步中的ID值替換URL中的<xxx-xxx-xxx-xxx-xxx>。

執行請求,返回的結果如下:

{
  "@odata.context": "https://microsoftgraph.chinacloudapi.cn/v1.0/$metadata#users(displayName,accountEnabled)/$entity",
  "displayName": "user name",
  "accountEnabled": true
}

返回結果中的accountEnabled就是最終所需要的結果!

【Azure Developer】中國區Azure環境中查看用户賬號是否可用(accountEnabled)的操作步驟_Graph_02

 

參考資料

 


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