我正在查看 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/