获得集团利用DirectoryServices

获得集团利用DirectoryServices

本文介绍了获得集团利用DirectoryServices.AccountManagement OU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AccountManagement列出所有组的组织单位。

I'd like to use AccountManagement to list all the groups in an Organizational Unit.

下面的代码片段可与的DirectoryServices,但我将不得不实例化GroupPrincipal与结果的的DirectoryEntry路径(这感觉就像一个肮脏的修复)。

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();

有没有人一个想法?

Has anyone an idea?

谢谢!

推荐答案

您可以设置 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:14