C# 获取AD OU列表
我希望能够从Active Directory中提取当前OU的列表。我已经在线查看了一些示例代码,但似乎无法实现这一点C# 获取AD OU列表,c#,active-directory,directoryservices,C#,Active Directory,Directoryservices,我希望能够从Active Directory中提取当前OU的列表。我已经在线查看了一些示例代码,但似乎无法实现这一点 string defaultNamingContext; DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"); defaultNamingContext = rootDSE.Properties["defaultNamingContext"].Value.To
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任何想法?
对于每个返回的搜索结果,我想将它们添加到一个组合框中。(应该不会太难)您不能在
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);
更新:我发现此筛选器错误-即使中的
objectCategory
显示为CN=organization Unit,…
,您需要在搜索中指定objectCategory=organizationalUnit
:
DirectorySearcher ouSearch = new DirectorySearcher(default,
"(objectCategory=organizationalUnit)",
null, SearchScope.Subtree);
我试着使用上面的建议进行搜索,这似乎是一个非常好的主意,尽管它有一个noob试图实现它。我将默认值更改为“domain”,我看不出有问题,我的问题是domain=System.DirectoryServices.DirectoryEntry,而不是LDAP://。。。虽然这是在它的路径属性中。我只是想为以后找到它的人添加。Server2003R2和更早版本没有索引
objectClass
,但是2008和更高版本有索引。答案一点也不反对!只是新的信息。资料来源: