代码之家  ›  专栏  ›  技术社区  ›  Douglas Anderson

如何获取Active Directory组中的组列表

  •  2
  • Douglas Anderson  · 技术社区  · 16 年前

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

    例如,我有一个名为TestGroup的组,在该组中有一个组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,但是没有返回任何结果。如何返回组中的组?

    1 回复  |  直到 16 年前
        1
  •  4
  •   Douglas Anderson    16 年前

    似乎如果不递归地执行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();
    
    推荐文章