如何使用目录搜索器从主活动目录获取子活动目录列表?

时间:2015-09-15 06:57:25

标签: directorysearcher

假设我有三个活动目录D1D2D3

D2 is group member of D1. 
D3 is group member of D2. 

因此,如果我想获取D1的群组成员列表,则结果应为D2,而不是D2D3

怎么做?

1 个答案:

答案 0 :(得分:1)

将Hashtable变量定义为全局变量。   Hashtable searchingGroups = null;

strGroupDN是主目录列表名称。

     private ArrayList getsubDL(string strGroupDN)
      {
        ArrayList allSubDL = new ArrayList();
        searchedGroups.Add(strGroupDN, strGroupDN);
        // get nested groups
        ArrayList Nestedgroups = new ArrayList();

        // find all nested groups in this group
        DirectorySearcher ds = new DirectorySearcher();
        ds.Filter = String.Format
                    ("(&(memberOf={0})(objectClass=group))", strGroupDN);

        foreach (SearchResult sr in ds.FindAll())
        {
            string grnm = sr.Properties["distinguishedName"][0].ToString();
            string grnsamn = sr.Properties["sAMAccountName"][0].ToString();
            Nestedgroups.Add(sr.Properties["distinguishedName"][0].ToString());
        }
        allSubDL.AddRange(Nestedgroups);


        return allSubDL;
    }




     public ArrayList GetsubDLList(string strGroupName)
      {
        ArrayList allSubDL = new ArrayList();
         searchedGroups = new Hashtable();

        // find group
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format
                        ("(&(objectCategory=group)(cn={0}))", strGroupName);
        search.PropertiesToLoad.Add("distinguishedName");
        SearchResult sru = null;
        DirectoryEntry group;

        try
        {
            sru = search.FindOne();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        group = sru.GetDirectoryEntry();

        allSubDL = getsubDL(group.Properties["distinguishedName"].Value.ToString());

        return allSubDL;
    }

allSubDL将为您提供子目录列表的详细信息。