c# - 如何查询所有组和组成员的 Active Directory?

标签 c# .net active-directory

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

当我选择一个组时,我希望它用经理的名字填充一个文本框以及另一个列表框,其中包含分配给该组的所有用户。我很难理解这个过程。有人可以帮帮我吗?

如果我得到一个完整的例子,我很确定我会更好地理解大局。时间差

最佳答案

如果您在 .NET 3.5 或更新版本上运行,您可以使用 PrincipalSearcher 和“query-by-example”主体进行搜索:

// 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 文章 Managing Directory Security Principals in the .NET Framework 3.5它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement

中的新功能

当您有一个给定的组时,您可以使用以下方法轻松获取其所有组成员:

// 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
   }
}

关于c# - 如何查询所有组和组成员的 Active Directory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9487517/

相关文章:

c# - 如何通过 SSL 从 LDAP 获取 DirectoryEntry?

c# - 如何在 ASP.NET Core 5 中使用 IdentityUser 和 IdentityRole 之间的隐式多对多

c# - Unity Player 正在向自己射击

jquery - 为所有动态生成的按钮获取相同的 ID

c# - 无法通过 TCP 发送或接收消息

java - 通过Java查找嵌套组中的LDAP用户

c# - 无法保存自定义集合用户设置

javascript - imageButton 的服务器标记格式不正确

javascript - 聚焦 Javascript OpenWindow 文本

active-directory - Active Directory LDAP示例