c# - 如何获取 Active Directory 组中的组列表

标签 c# .net active-directory

我正在尝试使用 .NET 获取 AD 组中的组列表。

例如,我有一个名为 TestGroup 的组,在该组内有一个 DomainAdministrators 组。

使用下面的代码,我可以获取所有用户,包括来自 DomainAdministrators 组的用户,但不能获取该组本身。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DomainName");
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "TestGroup");

ArrayList members = new ArrayList();

if (grp != null)
{
    foreach (Principal p in grp.GetMembers(true))
    {
        members.Add(p.Name)
    }

}   
grp.Dispose();
ctx.Dispose();

我尝试了 GetGroups 而不是 GetMembers,但它不会返回任何内容。如何返回群组中的群组?

最佳答案

似乎如果您不递归执行 GetMembers(传入 false),您将获得用户和组,并且只需要按 StructuralObjectClass 进行过滤。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DomainName"); 
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "TestGroup"); 

ArrayList users = new ArrayList();
ArrayList groups = new ArrayList(); 

if (grp != null) 
{ 
    foreach (Principal p in grp.GetMembers(false)) //set to false
    {
        if (p.StructuralObjectClass == "user")
            users.Add(p.Name);
        else if (p.StructuralObjectClass == "group")
            groups.Add(p.Name);
    }
}    
grp.Dispose(); 
ctx.Dispose();

关于c# - 如何获取 Active Directory 组中的组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2962539/

相关文章:

c# - 根据找到的数据修剪 3 维数组请求

.net - Visual Studio : wrong reference version

c# - 在 LinQ Lambda Join 中检索索引

powershell - 尝试将变量传递到 [adsisearcher] 的问题

c# - SqlBulkCopy - 数据源中 String 类型的给定值无法转换为指定目标列的 money 类型

c# - WPF 是否可以显式调用动画反转?

c# word-2007 加载项 : get the path and filename of current opened wordfile?

c# - 暂停使用 IOCP 的应用程序时创建的线程过多

arrays - 与 AD 中的职位列表匹配的用户列表

asp.net - 如何允许我的 Azure Web 应用程序访问 Active Directory?