Get members of an Active Directory group recursively, i.e. including subgroups

前端 未结 2 616
小鲜肉
小鲜肉 2020-12-30 11:49

Given a group like this in Active Directory:

MainGroup
  GroupA
    User1
    User2
  GroupB
    User3
  User4

I can easily determine if Us

2条回答
  •  粉色の甜心
    2020-12-30 12:31

        static List ad_find_all_members(string a_sSearchRoot, string a_sGroupDN, string[] a_asPropsToLoad)
        {
            using (DirectoryEntry de = new DirectoryEntry(a_sSearchRoot))
                return ad_find_all_members(de, a_sGroupDN, a_asPropsToLoad);
        }
    
        static List ad_find_all_members(DirectoryEntry a_SearchRoot, string a_sGroupDN, string[] a_asPropsToLoad)
        {
            string sDN = "distinguishedName";
            string sOC = "objectClass";
            string sOC_GROUP = "group";
            string[] asPropsToLoad = a_asPropsToLoad;
            Array.Sort(asPropsToLoad);
            if (Array.BinarySearch(asPropsToLoad, sDN) < 0)
            {
                Array.Resize(ref asPropsToLoad, asPropsToLoad.Length+1);
                asPropsToLoad[asPropsToLoad.Length-1] = sDN;
            }
            if (Array.BinarySearch(asPropsToLoad, sOC) < 0)
            {
                Array.Resize(ref asPropsToLoad, asPropsToLoad.Length+1);
                asPropsToLoad[asPropsToLoad.Length-1] = sOC;
            }
    
            List lsr = new List();
    
            using (DirectorySearcher ds = new DirectorySearcher(a_SearchRoot))
            {
                ds.Filter = "(&(|(objectClass=group)(objectClass=user))(memberOf=" + a_sGroupDN + "))";
                //ds.PropertiesToLoad.Clear();
                ds.PropertiesToLoad.AddRange(asPropsToLoad);
                //ds.PageSize = 1000;
                //ds.SizeLimit = 0;
                foreach (SearchResult sr in ds.FindAll())
                    lsr.Add(sr);
            }
    
            for(int i=0;i

提交回复
热议问题