![](/img/trans.png)
[英]C# Active Directory DirectorySearcher is only finding my own user
[英]Most efficient way to get child object properties from a DirectorySearcher result in Active Directory using C#
我正在嘗試找到最有效的方法,以從某些類型的對象中獲取屬性,這些對象的父OU已使用DirectorySearcher
查詢獲得。 這些對象的父級是用戶(直接或間接)在Active Directory中的成員的組。
我想我已經找到了一個很好的遞歸解決方案來獲取這些組,但是一旦獲得了結果集,我就不確定哪種最有效的數據獲取方法是。 現在,我正在使用每個結果的Path來獲取數據,就像我只是得到一個對象一樣。
我想知道是否有更快的方法,可能是通過添加到DirectorySeacher
的Filter
並將這些對象直接獲取到查詢結果中。 我要搜索的對象是對象,因此在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.