获取AD OU列表
我希望能够从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任何想法? 对于每个返回的搜索结果,我想将它们添加到combobox中。 (不应该太难)
您无法搜索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);
更新:
我发现这个filter是错误的 – 即使objectCategory
在CNSI浏览器中显示为CN=Organizational-Unit,.....
你需要在搜索它时指定objectCategory=organizationalUnit
才能成功:
DirectorySearcher ouSearch = new DirectorySearcher(default, "(objectCategory=organizationalUnit)", null, SearchScope.Subtree);
很好的解决方案,这应该是微软为这个链接提供的教程的一部分
http://msdn.microsoft.com/en-us/library/ms180890(VS.80).aspx