Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询AD以查找用户的所有组-缺少一个组_C#_.net_Search_Active Directory - Fatal编程技术网

C# 查询AD以查找用户的所有组-缺少一个组

C# 查询AD以查找用户的所有组-缺少一个组,c#,.net,search,active-directory,C#,.net,Search,Active Directory,下面是使用DirectorySearcher查询广告的代码,以获取用户的所有广告组 List<string> Groups = new List<string>(); //initialize the directory entry object DirectoryEntry dirEntry = new DirectoryEntry(ldapPath); //directory searcher

下面是使用
DirectorySearcher
查询广告的代码,以获取用户的所有广告组

        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;
列表组=新列表();
//初始化目录项对象
DirectoryEntry dirEntry=新的DirectoryEntry(ldapPath);
//目录搜索器
DirectorySearcher dirsearch=新的DirectorySearcher(dirEntry);
//输入过滤器
dirSearcher.Filter=string.Format(&(objectClass=user)(sAMAccountName={0})),用户名);
//获取搜索结果的属性成员
dirSearcher.PropertiesToLoad.Add(“memberOf”);
int-propCount;
SearchResult dirSearchResults=dirSearcher.FindOne();
propCount=dirSearchResults.Properties[“memberOf”].Count;
字符串dn;
国际均衡指数;
国际通信索引;

for(int i=0;i组可以是其他组的成员。您的用户可能不是直接成员,而是间接成员

在检索广告上的组时,我也会迭代子组的所有组

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


我已经编写了一个包含广告类的通用库(请参阅下载ZIP文件中“
/Tools/DirectoryServices/
”子文件夹中的类)。

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

在检索广告上的组时,我也会迭代子组的所有组

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


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

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

  • 获取用户的primaryGroupID属性,该属性是域中组对象的唯一序列ID
  • 构造组的objectSID(获取用户对象的objectSID并用primaryGroupID替换最后一个数字组)
  • 根据构造的SID获取组

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

  • 获取用户的primaryGroupID属性,该属性是域中组对象的唯一序列ID
  • 构造组的objectSID(获取用户对象的objectSID并用primaryGroupID替换最后一个数字组)
  • 根据构造的SID获取组

  • 那么我如何使用DirectorySearcher递归地找到它呢?我已经添加了一个指向我的答案的链接。好的。我已经递归地找到了父组。但是仍然有两个组(或文件夹?)“域用户”不在列表中。但如果我选中“memberOf”选项卡,它就在那里。域用户是一个内置组,对吗?我在这里遗漏了什么吗?在处理“广告内容”时有什么帮助是Softerra的LDAP浏览器。请参阅-请确保使用免费的2.6版,它仍然满足我的需要。在Softerra浏览器的帮助下,我可以看到更多关于组和用户等的详细信息。因此,如何使用DirectorySearcher递归查找它?我添加了一个指向我的答案的链接。好的。我已经递归找到了父级组。但仍然有两个组(或文件夹?)列表中没有“域用户”。但如果我选中“memberOf”选项卡,它就在那里。域用户是一个内置组,对吗?我在这里遗漏了什么吗?在处理“广告内容”时有什么帮助是Softerra的LDAP浏览器。请参阅-请务必使用免费的2.6版,它仍然满足我的需要。在Softerra浏览器的帮助下,我可以看到更多有关组和用户等的详细信息。可能基于您访问的服务器的配置及其角色/功能,请参阅本页的memberOf部分好的arnt from here-该memberOf不会返回用户的主要组。但是我可以得到primaryGroupID,它给出RID(?)我需要使用哪个组来获取广告组。有什么办法吗?可能基于您正在访问的服务器的配置及其角色/功能,请参阅本页的memberOf部分Okie。我从这里了解到-memberOf不会返回用户的主要组。但我可以获取primaryGroupID,它提供RID(?)我需要使用的组的名称来获得广告组。有什么办法吗?