【问题标题】:Get all the parent AD groups a user belongs to获取用户所属的所有父 AD 组
【发布时间】:2012-10-23 06:33:24
【问题描述】:

我想获取特定用户所属的所有 Active Directory 组。

我确实有脚本来获取用户的所有直接 AD 组(请参见下面的代码)。

但是,如何获取用户所属的每个直接 AD 组的所有父组?

例如我是名为IT Team Managers 的广告组的直接成员。此组是名为 IT Team National 等的父组的成员。如何从我的代码中获取此父组?

提前非常感谢!

DirectorySearcher ouSearch = new DirectorySearcher(entry);
ouSearch.Filter = "(&(objectClass=User)(sAMAccountName=" + username + "))";

//ouSearch.PropertiesToLoad.Add("samAccountName");
ouSearch.PropertiesToLoad.Add("memberOf");
ouSearch.SearchScope = SearchScope.Subtree;

SearchResult allOUS = ouSearch.FindOne();

//foreach (string g in allOUS.Properties["memberOf"])
{
    equalsIndex = g.IndexOf("=", 1);
    commaIndex = g.IndexOf(",", 1);

    if (equalsIndex == -1)
    {
       return null;
    }

    groupNames.Append(g.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
    groupNames.Append(",");
}

【问题讨论】:

    标签: c# .net active-directory ldap directoryservices


    【解决方案1】:

    如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:

    基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

    // set up domain context
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
    
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
    
    if(user != null)
    {
       // get the "authorization groups" the user is a member of - recursively
       var authGroups = user.GetAuthorizationGroups();
    
       // iterate over groups
       foreach(Principal p in authGroups)
       {
          // do something with groups ....       
       }
    }
    

    新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!

    【讨论】:

    • 非常感谢您的回复。它可以工作-但是当我运行它时,返回约 100 个 AD 组的速度太慢了...几乎 1 分钟...有什么方法可以加快速度或如何优化/过滤组以使其不返回这么多组?
    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多