简体   繁体   English

使用C#从Active Directory中从DirectorySearcher获取子对象属性的最有效方法

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

I am trying to find the most efficient way possible to get properties from certain types of objects whose parent OUs have been gethered using a DirectorySearcher query. 我正在尝试找到最有效的方法,以从某些类型的对象中获取属性,这些对象的父OU已使用DirectorySearcher查询获得。 These object's parents are groups that users are a member of (directly or indirectly) in Active Directory. 这些对象的父级是用户(直接或间接)在Active Directory中的成员的组。

I think I have found a good recursive solution to get these groups, however once I have my result set, I'm not sure what the most efficient way to fetch the data is. 我想我已经找到了一个很好的递归解决方案来获取这些组,但是一旦获得了结果集,我就不确定哪种最有效的数据获取方法是。 Right now I'm using each result's Path to fetch the data like I would if I was just getting a single object. 现在,我正在使用每个结果的Path来获取数据,就像我只是得到一个对象一样。

I'm wondering if there is a faster way to do this, possibly by adding to my DirectorySeacher 's Filter and getting these objects directly in my query results. 我想知道是否有更快的方法,可能是通过添加到DirectorySeacherFilter并将这些对象直接获取到查询结果中。 The objects I'm searching for are objects so it seems the closest I can get to them in the DirectorySearcher query is going to be their parent OU. 我要搜索的对象是对象,因此在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);
    }
}

I'm always skeptic about recursion when web requests or queries are being performed. 当执行Web请求或查询时,我总是对递归持怀疑态度。 But if it works for you, great!. 但是,如果它对您有用,那就太好了!
You can use DirectorySearcher for child nodes to further narrow down the resutls. 您可以对子节点使用DirectorySearcher来进一步缩小结果范围。 Setup a DirectorySearcher: 设置DirectorySearcher:

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

You can add more parameters depending on how the ActiveDirectory is set up. 您可以根据ActiveDirectory的设置方式添加更多参数。 Next you can specify the properties that you need in the results 接下来,您可以在结果中指定所需的属性

_Search.PropertiesToLoad.Add("distinguishedname");

Use FindAll() method to get all objects and iterate them using foreach loop: 使用FindAll()方法获取所有对象并使用foreach循环对其进行迭代:

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

}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM