我正在尝试使用 .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/