gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:21:01 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com