如何从 Active Directory 获取组织单位列表?
我研究过 DirectoryServices 类,它似乎是我需要什么,但我似乎找不到获取组织单位集合所需的类/方法。
大家能给一些建议吗?
I've looked into the DirectoryServices class and it seems to be what I need, but I can't seem to find the classes/methods needed to fetch a collection of Organizational Units.
Can you guys give some suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用
System.DirectoryServices
中的适当DirectorySearcher
,并且需要搜索organizationalUnit
AD 类(我建议基于objectCategory
是单值和索引的 - 比使用objectClass
快得多) - 像这样:You need to use an appropriate
DirectorySearcher
fromSystem.DirectoryServices
, and you need to search for theorganizationalUnit
AD class (I would recommend searching based on theobjectCategory
which is single-valued and indexed - much faster than usingobjectClass
) - something like this:我知道这个线程有点旧,但我最近创建了一种比 DirectorySearcher 提供的更有效的通过 DirectoryEntries 进行操作的方法,并且希望分享,因为这是 Google 上的最高结果。此示例根据最初指定的起点复制 OU 结构。
传递给第一个构造函数的 DN 路径应采用以下格式:“LDAP://OU=StartingOU,DC=test,DC=com”
要构建,您需要执行以下操作:
I know this thread is a little old, but I recently created a more efficient way of maneuvering through DirectoryEntries than the DirectorySearcher provides and wanted to share since this was the top result on Google. This example replicates the OU structure based on an initially specified starting point.
The DN path passed to the first constructor should be in the format "LDAP://OU=StartingOU,DC=test,DC=com"
To build, all you need to do is the following:
Jamie 提供的代码运行良好。要回答 MacGuyver 的问题,为了使用 Cast 扩展,您需要在代码中包含对 System.Linq 的引用。我使用此代码来填充在我的 AD 环境中显示 OU 的 TreeView:
这是一个带有 TreeView (treeview1) 和 Button (button1) 的简单 Windows 窗体应用程序 - 希望它有帮助!
The code provided by Jamie works nicely. To answer MacGuyver's question, in order to use the Cast extension, you need to include a reference to System.Linq in your code. I'm using this code to populate a TreeView showing the OUs in my AD environment:
This is on a simple Windows Forms app with a TreeView (treeview1) and a Button (button1) - hope it helps!
您是否看过 DirectorySearcher 方法?
以下是 MSDN 和bytes.com 。
Have you looked at the DirectorySearcher method?
Here are some examples at MSDN and bytes.com.
这是我的解决方案并且它正在工作:
This is my solution and it's working: