C# 使用 DirectoryServices.AccountManagement 从 OU 获取组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1927261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Groups From OU using DirectoryServices.AccountManagement
提问by teebot
I'd like to use AccountManagement to list all the groups in an Organizational Unit.
我想使用 AccountManagement 列出组织单位中的所有组。
The following snippet works with DirectoryServices but I would have to instanciate GroupPrincipal with the DirectoryEntry path in the result (which feels like a dirty fix).
以下代码段适用于 DirectoryServices,但我必须在结果中使用 DirectoryEntry 路径实例化 GroupPrincipal(这感觉像是一个肮脏的修复)。
DirectoryEntry root = new DirectoryEntry("LDAP://OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local")
DirectorySearcher ds = new DirectorySearcher(root);
ds.Filter = "(objectCategory=group)";
SearchResultCollection results = ds.FindAll();
Has anyone an idea?
有人有想法吗?
Thanks!
谢谢!
采纳答案by Per Noalt
You can set the PrincipalContext
to the OU where you want to start the search and use the PrincipalSearcher
-class in System.DirectoryService.AccountManagement
to accomplish what you need, like this:
您可以将 设置为PrincipalContext
要开始搜索的 OU 并使用PrincipalSearcher
-class inSystem.DirectoryService.AccountManagement
来完成您需要的操作,如下所示:
PrincipalContext yourOU = new PrincipalContext(ContextType.Domain, "mycompany.local", "OU=Marketing,OU=Operations,OU=Applications,DC=mycompany,DC=local");
GroupPrincipal findAllGroups = new GroupPrincipal(yourOU, "*");
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach(var group in ps.FindAll())
{
Console.WriteLine(group.DistinguishedName);
}
Console.ReadLine();