【问题标题】:Most efficient way to get child object properties from a DirectorySearcher result in Active Directory using C#使用 C# 在 Active Directory 中从 DirectorySearcher 结果获取子对象属性的最有效方法
【发布时间】:2017-11-17 21:36:55
【问题描述】:

我试图找到最有效的方法来从某些类型的对象中获取属性,这些对象的父 OU 已使用 DirectorySearcher 查询进行了聚合。这些对象的父对象是用户在 Active Directory 中(直接或间接)所属的组。

我想我已经找到了一个很好的递归解决方案来获取这些组,但是一旦我有了我的结果集,我不确定获取数据的最有效方法是什么。现在我正在使用每个结果的路径来获取数据,就像我只是获取一个对象一样。

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

【问题讨论】:

    标签: c# active-directory directory ldap


    【解决方案1】:

    在执行 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;
    
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多