如何从azure AD B2C获取用户组信息

时间:2017-07-12 08:31:09

标签: azure-active-directory microsoft-graph azure-ad-b2c

我使用Microsoft Graph获取用户信息

https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_list

以下是访问用户信息的代码:

  ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
  AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resourceId, clientCred);
  string token = authenticationResult.AccessToken;
  Debug.WriteLine("token=" + token);   

  var responseString = String.Empty;
  string[] scopes = { "User.Read" };

  using (var client = new HttpClient())
  {
    string requestUrl = "https://graph.microsoft.com/v1.0/users?$select=id,givenName,surname";

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
    Debug.WriteLine(request.ToString());

    HttpResponseMessage response = client.SendAsync(request).Result;

    responseString = response.Content.ReadAsStringAsync().Result;

    Debug.WriteLine(responseString);
  }

输出:

 {    
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users(id,givenName,surname)",
"value": [
    {
        "id": "000000000-0000-0000-000-000000000",
        "givenName": "XXX",
        "surname": "XXX"
    }, {
        "id": "000000000-0000-0000-000-000000000",
        "givenName": "XXX",
        "surname": "XXX"
    }
]
 }

如何获得用户组?

1 个答案:

答案 0 :(得分:1)

您可以为用户调用getMemberGroups操作以获取他们的群组:https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_getmembergroups

您需要提出这样的请求:

POST https://graph.microsoft.com/v1.0/users/user-id-here/getMemberGroups
Content-type: application/json
Content-length: 33

{
  "securityEnabledOnly": true
}

securityEnabledOnly参数定义它是否应仅返回安全组。例如,将其设置为false也将返回用户的Office 365组成员身份。

另一种方法是使用memberOf导航属性:

GET https://graph.microsoft.com/v1.0/users/user-id-here/memberOf

这将返回用户是直接成员的组和目录角色。