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