簡體   English   中英

使用C#從Active Directory中從DirectorySearcher獲取子對象屬性的最有效方法

[英]Most efficient way to get child object properties from a DirectorySearcher result in Active Directory using C#

我正在嘗試找到最有效的方法,以從某些類型的對象中獲取屬性,這些對象的父OU已使用DirectorySearcher查詢獲得。 這些對象的父級是用戶(直接或間接)在Active Directory中的成員的組。

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

我想知道是否有更快的方法,可能是通過添加到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);
    }
}

當執行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;

}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM