获取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 有什么想法吗? 对于每个返回的搜索结果,我想将它们添加到组合框中。 (不应该太难)
I am looking to be able to pull a list of current OU's from Active Directory I have been looking at some example code online for sometime, but O don't seem to be able to get this to work.
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();
}
The error I get is There provider does not support searching and cannot search LDAP://RootDSE Any Ideas?
for each of those returned search results I want to add them to a combo box. (shouldn't be too hard)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在
LDAP://RootDSE
级别进行搜索 - 这只是一个包含一些内容的“信息”地址。它并不真正代表目录中的任何位置。您需要首先绑定到默认命名上下文:完成此操作后,您应该可以找到域中的所有 OU。
为了加快速度,我建议不要使用
objectClass
进行搜索 - 该属性未在 AD 中建立索引。请改用objectCategory
,它已编入索引:更新:
我发现此过滤器是错误的 - 即使 ADSI 浏览器,您需要在搜索中指定
objectCategory=organizationalUnit
才能成功:You cannot search on the
LDAP://RootDSE
level - that's just an "informational" address with some stuff. It doesn't really represent any location in your directory. You need to bind to the default naming context first:Once you do that, you should be OK to find all OU's in your domain.
And in order to speed things up, I would recommend not searching using
objectClass
- that property is not indexed in AD. UseobjectCategory
instead, which is indexed:UPDATE:
I discovered this filter is wrong - even though the
objectCategory
is shown asCN=Organizational-Unit,.....
in the ADSI browser, you need to specifyobjectCategory=organizationalUnit
in the search for it to succeed: