0

我正在尝试从活动目录中获取 OU 的列表。不幸的是,即使我知道“myApp”域组件中有 2 个 OU,我的搜索也总是没有任何结果。

using (var entry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password)) {
    using (var searcher = new DirectorySearcher()) {
        searcher.SearchRoot = entry;
        searcher.Filter = "(objectCategory=Organizational-Unit)";
        searcher.PropertiesToLoad.Add("name");

        //foo never gets results.  :(
        var foo = searcher.FindAll();
    }
}

我尝试按照上一个 StackOverflow问题中的代码进行操作,但是没有运气。

4

3 回答 3

1

我用这样的东西。它使用路径检索字典名称中的所有 OU,只需正确更改 SearchScope。

public Dictionary<string, string> GetOUInfo(SearchScope eSearchScope)
    {
        Dictionary<string, string> retValues = new Dictionary<string, string>();

        try
        {
            DirectoryEntry oDirectoryEntry = new DirectoryEntry("LDAP://myServer:1111/DC=myApp,DC=myDomain,DC=com", Username, Password);
            DirectorySearcher oDirectorySearcher = new DirectorySearcher(oDirectoryEntry,
                "(objectCategory=organizationalUnit)", null, eSearchScope);

            SearchResultCollection oSearchResultCollection = oDirectorySearcher.FindAll();
            foreach (SearchResult item in oSearchResultCollection)
            {
                string name = item.Properties["name"][0].ToString();
                string path = item.GetDirectoryEntry().Path;
                retValues.Add(path, name);
            }
        }
        catch (Exception ex)
        {
        }

        return retValues;
    }
于 2014-03-12T17:51:20.380 回答
0

1)您确定基础搜索"DC=myApp,DC=myDomain,DC=com"吗?是"myApp"域组件吗?

2)您可以尝试指定搜索范围吗?

searcher.SearchScope = SearchScope.Subtree;

3)"(objectCategory=Organizational-Unit)"是 Active-Directory 理解的快捷方式,但实际上 objectCategory 属性是一个专有名称 (DN),而 OU 的真正值是 : CN=Organizational-Unit,CN=Schema,CN=Configuration,domain root DN

你能试试这个"(objectClas=Organizational-Unit)"更常见的过滤器吗?


你可以在命令行上试试这个吗?

C:\temp>ldifde -f c:\temp\out.txt -d "DC=myApp,DC=myDomain,DC=com" -r "(objectClass=organizationalUnit)"
于 2013-04-04T04:04:26.600 回答
0

使用它会起作用

PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.com", "OU=Marketing,OU=Corporate,DC=mycompany,DC=com");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach (var group in ps.FindAll())
{
Console.WriteLine(group.DistinguishedName);
}
于 2017-03-02T05:29:58.570 回答