如何在Active Directory中查询所有组和组成员? [英] How to query Active Directory for all groups and group members?

查看:305
本文介绍了如何在Active Directory中查询所有组和组成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 DirectoryServices 命名空间,我试图获取广告中所有组的列表,并将它们加载到列表框中。

I'm looking at the DirectoryServices namespace and I'm trying to get a list of all groups in the AD and load them into a listbox.

当我选择一个组,我希望它在一个文本框中填入经理的姓名,并在另一个列表框中填入分配给该组的所有用户。我很难全神贯注于此过程。有人可以帮我吗?

When I select a group, I want it to fill a text box with the manager's name as well another listbox with all users assigned to that group. I'm having a hard time wrapping my head around the process. Could someone help me out?

如果我有一个完整的例子,我很确定自己会更好地理解大局。 TIA

If I get a full example of this, I'm fairly sure that I'll understand the bigger picture a lot better. TIA

推荐答案

如果您在.NET 3.5或更高版本上运行,则可以使用 PrincipalSearcher 和按示例查询主体来进行搜索:

If you're running on .NET 3.5 or newer, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);

// find all matches
foreach(var found in srch.FindAll())
{
     GroupPrincipal foundGroup = found as GroupPrincipal;

     if(foundGroup != null)
     {
        // do whatever you need to do, e.g. put name into a list of strings or something
     }
}

如果还没有-绝对阅读MSDN文章在.NET Framework中管理目录安全主体3.5 很好地展示了如何充分利用 System.DirectoryServices.AccountManagement

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

当您拥有一个给定的组时,可以使用以下命令轻松获得其所有组成员:

When you have a given group, you can easily get all its group members by using:

// find the group in question (or load it from e.g. your list)
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");

// if found....
if (group != null)
{
   // iterate over members
   foreach (Principal p in group.GetMembers())
   {
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
      // do whatever you need to do to those members
   }
}

这篇关于如何在Active Directory中查询所有组和组成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆