【问题标题】:How to check if user is part of an AD group? (even group within a group)如何检查用户是否属于 AD 组? (甚至组内组)
【发布时间】:2016-05-23 07:03:00
【问题描述】:

我需要检查登录到我正在制作的控制台应用程序的用户是否属于 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))
   {
       ...
   }
}

【问题讨论】:

    标签: authentication active-directory active-directory-group


    【解决方案1】:

    查看用户是否是嵌套组成员的一种方法是获取组recursively 中的所有用户。我正在使用您的代码中的user 和group:

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

    【讨论】:

      猜你喜欢
      • 2012-08-15
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多