我想使用 AccountManagement 列出组织单位中的所有组.
I'd like to use AccountManagement to list all the groups in an Organizational Unit.
以下代码段适用于 DirectoryServices,但我必须在结果中使用 DirectoryEntry 路径实例化 GroupPrincipal(这感觉像是一个肮脏的修复).
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).
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();
有人有想法吗?
谢谢!
您可以将 PrincipalContext 设置为要开始搜索的 OU 并使用 PrincipalSearcher-System.DirectoryService.AccountManagement 中的类来完成你需要的,像这样:
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 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();
这篇关于使用 DirectoryServices.AccountManagement 从 OU 获取组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
为什么我不应该总是在 C# 中使用可空类型Why shouldn#39;t I always use nullable types in C#(为什么我不应该总是在 C# 中使用可空类型)
C# HasValue vs !=nullC# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET:空值和 DbNull —— 有没有更高效的语法C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有没有更高效的语法?)
如何在c#中将空值设置为int?How to set null value to int in c#?(如何在c#中将空值设置为int?)
使用 Min 或 Max 时如何处理 LINQ 中的空值?How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 时如何处理 LINQ 中的空值?)
在 C# 中如果不为 null 的方法调用Method call if not null in C#(在 C# 中如果不为 null 的方法调用)