繁体   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