【问题标题】:Acquiring AD OU list获取 AD OU 列表
【发布时间】:2010-05-25 09:45:57
【问题描述】:

我希望能够从 Active Directory 中提取当前 OU 的列表我一直在网上查看一些示例代码一段时间,但 O 似乎无法让它工作。

        string defaultNamingContext;

        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
        defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
        DirectorySearcher ouSearch = new DirectorySearcher(rootDSE, "(objectClass=organizationalUnit)", 
            null, SearchScope.Subtree);

        MessageBox.Show(rootDSE.ToString());
        try
        {
            SearchResultCollection collectedResult = ouSearch.FindAll();
            foreach (SearchResult temp in collectedResult)
            {
                comboBox1.Items.Add(temp.Properties["name"][0]);
                DirectoryEntry ou = temp.GetDirectoryEntry();
            }

我得到的错误是那里提供者不支持搜索并且无法搜索 LDAP://RootDSE 有什么想法吗? 对于每个返回的搜索结果,我想将它们添加到组合框中。 (不应该太难)

【问题讨论】:

    标签: c# active-directory directoryservices


    【解决方案1】:

    您不能在 LDAP://RootDSE 级别上搜索 - 这只是一个包含一些内容的“信息”地址。它并不真正代表您目录中的任何位置。您需要先绑定到默认命名上下文:

    string defaultNamingContext;
    
    DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
    defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.ToString();
    
    DirectoryEntry default = new DirectoryEntry("LDAP://" + defaultNamingContext);
    
    DirectorySearcher ouSearch = new DirectorySearcher(default, 
                                         "(objectClass=organizationalUnit)", 
                                         null, SearchScope.Subtree);
    

    完成此操作后,您应该可以找到域中的所有 OU。

    为了加快速度,我建议不要使用objectClass 进行搜索 - 该属性没有在 AD 中编入索引。请改用objectCategory,它已编入索引:

    DirectorySearcher ouSearch = new DirectorySearcher(default, 
                                         "(objectCategory=Organizational-Unit)", 
                                         null, SearchScope.Subtree);
    

    更新:
    我发现这个过滤器是错误的——即使objectCategoryADSI browser 中显示为CN=Organizational-Unit,.....,您需要在搜索中指定objectCategory=organizationalUnit 才能成功:

    DirectorySearcher ouSearch = new DirectorySearcher(default, 
                                         "(objectCategory=organizationalUnit)", 
                                         null, SearchScope.Subtree);
    

    【讨论】:

    • 我已经尝试使用您上面的建议进行搜索,这似乎是一个非常好的主意,尽管它有一个菜鸟试图实现它。我将默认值更改为“域”,但我看不出有问题,我的问题是域 = System.DirectoryServices.DirectoryEntry,而不是 LDAP://...尽管它在其 Path 属性中。
    • 只是想我会为以后发现这个的人添加。 Server 2003 R2 及更早版本没有索引objectClass,但 2008 及更高版本有。不反对答案!只是新信息。来源:msdn.microsoft.com/en-us/library/ms675095(v=vs.85).aspx
    猜你喜欢
    • 2017-08-20
    • 1970-01-01
    • 2018-10-04
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多