Authentication 如何检查用户是否属于广告组?(组中的偶数组)

Authentication 如何检查用户是否属于广告组?(组中的偶数组),authentication,active-directory,active-directory-group,Authentication,Active Directory,Active Directory Group,我需要检查登录到我正在制作的控制台应用程序的用户是否是DL的一部分(我们称之为DL-a) 有些用户不是DL-A的直接成员,而是DL-A成员的其他DL的直接成员。我使用的代码只检查用户直接属于的组。我怎么检查这个 PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain); UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username); Group

我需要检查登录到我正在制作的控制台应用程序的用户是否是DL的一部分(我们称之为DL-a)

有些用户不是DL-A的直接成员,而是DL-A成员的其他DL的直接成员。我使用的代码只检查用户直接属于的组。我怎么检查这个

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "DL-A");

if (user != null)
{
   if (user.IsMemberOf(group))
   {
       ...
   }
}

查看用户是否是嵌套组成员的一种方法是递归地从组中获取所有用户。我正在使用您代码中的
用户

....

if (group != null)
{
    var users = group.GetMembers(true); //this will get nested users
    var contains = users.Contains(user);
    if (contains)
    {
       //user found
    }
}

...

查看用户是否是嵌套组成员的一种方法是递归地从组中获取所有用户。我正在使用您代码中的
用户

....

if (group != null)
{
    var users = group.GetMembers(true); //this will get nested users
    var contains = users.Contains(user);
    if (contains)
    {
       //user found
    }
}

...