【问题标题】:Querying AD for finding all groups of a user - Missing one group查询 AD 以查找用户的所有组 - 缺少一个组
【发布时间】:2010-12-17 11:50:00
【问题描述】:

我有以下代码使用DirectorySearcher 查询 AD 以获取用户的所有 AD 组。

        List<string> Groups = new List<string>();

        //initialize the directory entry object 
        DirectoryEntry dirEntry = new DirectoryEntry(ldapPath);

        //directory searcher
        DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry);

        //enter the filter
        dirSearcher.Filter = string.Format("(&(objectClass=user)(sAMAccountName={0}))", username);

        //get the member of properties for the search result
        dirSearcher.PropertiesToLoad.Add("memberOf");
        int propCount;
        SearchResult dirSearchResults = dirSearcher.FindOne();
        propCount = dirSearchResults.Properties["memberOf"].Count;
        string dn;
        int equalsIndex;
        int commaIndex;
        for (int i = 0; i <= propCount - 1; i++)
        {
            dn = dirSearchResults.Properties["memberOf"][i].ToString();

            equalsIndex = dn.IndexOf("=", 1);
            commaIndex = dn.IndexOf(",", 1);
            if (equalsIndex == -1)
            {
                return null;
            }
            if (!Groups.Contains(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1)))
            {
                Groups.Add(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
            }
        }

        return Groups;

但是当我检查 AD 中的“memberof”选项卡中的用户时,我还有一个额外的组“域用户”,我没有通过此代码。

有什么想法吗?为什么我在“memberof”集合中没有得到“域用户”?

【问题讨论】:

标签: c# .net search active-directory


【解决方案1】:

组可以是其他组的成员。也许您的用户不是直接成员,而只是间接成员?

在检索 AD 上的组时,我也会为子组迭代所有组。

请注意,您可能会遇到无休止的递归,因为组可以(间接)相互包含。我很难发现这一点:-(现在我记得“全局”列表中的每个处理组只处理一次以避免这种情况)。

我用一些通用库编写了CodeProject article,其中也包含 AD 类。 (请参阅下载的 ZIP 文件中“/Tools/DirectoryServices/”子文件夹中的类)。

【讨论】:

  • 那么如何使用 DirectorySearcher 递归查找呢?
  • 我已经为我的答案添加了一个链接。
  • 好吧。我已经递归找到了父组。但是仍然有两个组(或文件夹?)“域用户”没有出现在列表中。但是,如果我检查“memberOf”选项卡,它就在那里。域用户是一个内置组对吗?我在这里错过了什么吗?
  • 在处理“AD 东西”时帮助我的是 Softerra 的 LDAP 浏览器。请参阅ldapbrowser.com/download.htm - 请务必使用免费的 2.6 版,它仍然足以满足我的需求。在 Softerra 浏览器的帮助下,我可以看到更多关于组和用户等的详细信息。
【解决方案2】:

这是旧的,但对于其他搜索的人来说,memberof 属性缺少“域用户”的原因是因为那是 AD 对象的 PRIMARY GROUP。要查找用户的主要组,您需要:

  1. 获取用户的primaryGroupID属性,即域内组对象的唯一序列号
  2. 构造组的objectSID(取用户对象的objectSID并将最后一位数字组替换为primaryGroupID)
  3. 根据构造的 SID 获取组

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多