从DirectorySearcher获取子对象属性的最有效方法导致在Active Directory中使用C#
问题描述:
我试图找到最有效的方式来获取某些类型的对象的父OU已使用DirectorySearcher
查询来获取属性。这些对象的父母是用户在Active Directory中(直接或间接)成员的组。从DirectorySearcher获取子对象属性的最有效方法导致在Active Directory中使用C#
我想我已经找到了一个很好的递归解决方案来获得这些组,但是一旦我有了我的结果集,我不知道最有效的方式来获取数据。现在,我正在使用每个结果的路径来获取数据,就像我只是获取单个对象一样。
我想知道是否有更快的方法来做到这一点,可能是通过添加到我的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;
}
希望这有助于。