【问题标题】:How to get a list of groups in an Active Directory group如何获取 Active Directory 组中的组列表
【发布时间】:2010-06-03 00:19:54
【问题描述】:

我正在尝试使用 .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的值。 GetMembers 返回属于组的 AD 对象。 GetGroups 是当前 AD 对象所属的组成员

标签: c# .net active-directory


【解决方案1】:

似乎如果您不递归执行 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();

【讨论】:

    猜你喜欢
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多