2017-11-17 272 views
0

我試圖找到最有效的方式來獲取某些類型的對象的父OU已使用DirectorySearcher查詢來獲取屬性。這些對象的父母是用戶在Active Directory中(直接或間接)成員的組。從DirectorySearcher獲取子對象屬性的最有效方法導致在Active Directory中使用C#

我想我已經找到了一個很好的遞歸解決方案來獲得這些組,但是一旦我有了我的結果集,我不知道最有效的方式來獲取數據。現在,我正在使用每個結果的路徑來獲取數據,就像我只是獲取單個對象一樣。

我想知道是否有更快的方法來做到這一點,可能是通過添加到我的DirectorySeacherFilter並直接在我的查詢結果中獲取這些對象。我正在搜索的對象是對象,所以看起來最接近我可以在DirectorySearcher查詢中找到它們,它們將成爲它們的父OU。

foreach (SearchResult result in matchingADGroups) 
{ 
    // Here I need to get result's child object properties(could be multiple children) 
    DirectoryEntry entry = new DirectoryEntry("LDAP://" + result.Path.Substring(7)); 

    foreach(DirectoryEntry child in entry.Children) 
    { 
     Shortcut shortcut = new Shortcut(); 
     shortcut.DisplayName = (string)child.Properties["myDisplayName"].Value; 
     shortcut.Id = (string)child.Properties["myId"].Value; 

     shortcuts.Add(shortcut); 
    } 
} 

回答

0

我一直懷疑Web請求或查詢執行時發生遞歸。但如果它適合你,太棒了!
您可以使用DirectorySearcher作爲子節點來進一步縮小分值。設置DirectorySearcher:

DirectorySearcher _Search = new DirectorySearcher(entry); 
_Search.Filter = "(&(objectCategory=person)(objectClass=user))";//can add more parameters 

根據ActiveDirectory的設置方式,您可以添加更多參數。 接下來,您可以指定在結果需要

_Search.PropertiesToLoad.Add("distinguishedname"); 

使用的FindAll()方法來獲取所有對象和使用foreach循環遍歷它們的屬性:

foreach (var result in _Search.FindAll()){ 
     //DO whatever you want here 
     Shortcut shortcut = new Shortcut(); 
     shortcut.DisplayName = result.DisplayName; 

} 

希望這有助於。

相關問題