2016-10-27 67 views

回答

2

根據我的理解,要檢索組成員遞歸,我們只需要檢測成員的類型。如果該成員是組,我們可以再次請求成員。

我也使用Azure的AD圖客戶端,供大家參考寫了一個演示:

public void testPrintMembersRecursively() 
{ 
    var client = GraphHelper.CreateGraphClient(); 
    var group = (Group)client.Groups.ExecuteAsync().Result.CurrentPage.First(g => g.DisplayName == "group4"); 
    var groupFetcher = client.Groups.GetByObjectId(group.ObjectId); 

    PrintMembersRecursively(groupFetcher, 0); 
} 

public void PrintMembersRecursively(Microsoft.Azure.ActiveDirectory.GraphClient.IGroupFetcher groupFetcher, int level) 
{ 
    var membersResoult = groupFetcher.Members.ExecuteAsync().Result; 
    Print(level, membersResoult); 

    while (membersResoult.MorePagesAvailable) 
    { 
     membersResoult=membersResoult.GetNextPageAsync().Result; 
     Print(level,membersResoult); 
    } 
} 

private void Print(int level, IPagedCollection<IDirectoryObject> membersResoult) 
{ 
    var members = membersResoult.CurrentPage; 

    foreach (var obj in members) 
    { 
     var user = obj as Microsoft.Azure.ActiveDirectory.GraphClient.User; 
     if (user != null) 
      Console.WriteLine(String.Format("{0," + level * 8 + "}", user.DisplayName)); 
     else 
     { 
      var groupMember = obj as Microsoft.Azure.ActiveDirectory.GraphClient.Group; 
      if (groupMember != null) 
      { 
       Console.WriteLine(String.Format("{0," + level * 8 + "}", groupMember.DisplayName)); 
        PrintMembersRecursively(GetGroupById(groupMember.ObjectId), level + 1); 
      } 
     } 
    } 
} 


public static ActiveDirectoryClient CreateGraphClient() 
{ 
    string accessToken = ""; 
    string tenantId = "xxx.onmicrosoft.com"; 
    string graphResourceId = "https://graph.windows.net"; 

    Uri servicePointUri = new Uri(graphResourceId);  
    Uri serviceRoot = new Uri(servicePointUri, tenantId); 

    ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async() => await Task.FromResult(accessToken)); 

    return activeDirectoryClient; 
} 
+0

謝謝您的回答,但不幸的是我不能嘗試這個,因爲這是照顧,但因爲你已經在你結束我試了一下將它標記爲已接受的答案。 –

相關問題