如何在c#中的System.DirectoryServices.Protocol中获取嵌套组(子组)

时间:2017-04-05 06:43:32

标签: c# active-directory ldap directoryservices ldap-query

我有一个函数,它将参数作为组的11.89.56获取,并使用_id: 121.89.2 & 11.89.56 查询和Distringuished name返回给定组中的嵌套组或组。当我使用SearchRequest时,代码工作正常但在使用SearchResponse类时失败。有必要使用DirectoryEntry类。请在下面找到代码段:

LdapConnection

在回复中它没有给出任何东西。 (如果是LdapConnection,它确实提供了结果)

2 个答案:

答案 0 :(得分:0)

我认为你这太难了。假设您使用的是Microsoft Active Directory,并且您希望获得属于现有组的成员的组,我认为您可以使用过滤器,例如:

(memberOf:1.2.840.113556.1.4.1941:=CN=GroupOne,OU=Security Groups,OU=Groups,DC=YOURDOMAIN,DC=NET) 

如果您想要所有成员,包括用户:

(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:=CN=GroupOne,OU=Security Groups,OU=Groups,DC=YOURDOMAIN,DC=NET)

或仅检索用户:

var json=[{"name":"Lenovo Thinkpad 41A4298","website":"google"},
{"name":"Lenovo Thinkpad 41A2222","website":"google"},
{"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
{"name":"Lenovo Thinkpad 41A424448","website":"google"},
{"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
{"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
{"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];


 var as=$(json).filter(function (i,n){return n.website==='yahoo'});



for (var i=0;i<as.length;i++)
  {
    alert(as[i].name +"         "+as[i].website)
}

ldapwiki

获得大部分内容

如果有效,请告诉我们。

答案 1 :(得分:0)

对于任何组,我们都可以使用以下查询获取组对象: -

public static void GetUsersCorrespondingToGroupChild(string strGroupDN)         {

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.DistinguishedName = strGroupDN;
        searchRequest.Filter = String.Format("(&(objectCategory=Group)(CN={0}))", strGroupDN.ToString().Split('=')[1].Split(',')[0]);
        SearchResponse response =
  (SearchResponse)ldap.SendRequest(searchRequest);
        if (response != null && response.Entries.Count > 0)
        {
            SearchResultEntry obj = response.Entries[0];//I get group object here
            if (obj.Attributes["member"] != null)
            {


                var childCount = ((System.Collections.CollectionBase)(obj.Attributes["member"])).Count;

                for (int i = 0; i < childCount; i++)
                {

                    string groupName = obj.Attributes["member"][i].ToString();//I get all members in which i have to find subgroups
                    List<string> localGroupList = new List<string>();
                    if (groupName.Contains("OU=Groups"))
                    {
                        var attributes = obj.Attributes.AttributeNames;
                        string attributesstr = string.Empty;
                        foreach (var item in attributes)
                        {
                            attributesstr = attributesstr + "," + item;
                        }
                        _subGroupList.Add(groupName.ToString().Split('=')[1].Split(',')[0] + "  :  " + attributesstr);
                        count_Children++;


                    }



                }


            }
        }

    }

因此对于子组,我只需要获取属性[&#34; member&#34;]查询以返回所有用户和组,然后我必须检索与其对应的组。