Get Count of members in a AD Group using PrincipalSearcher

£可爱£侵袭症+ 提交于 2019-12-13 02:27:53

问题


Env : Visual Studio 2013, FrameWork 4.5, Telerik Controls, C#, WebForm application

Using : System.DirectoryServices and System.DirectoryServices.AccountManagement

I'm making a search tools so a user can search for a active directory group name in multiple forest/domain.

The search return a list of 1 or more group and I put that list in a RadGrid (Telerik). Each row of the grid is a AD Group. I would like to display an additional information that show the user how many(count?) members(users) there is in that group.

private List<AdGroup> GetListOfGroupAD(string domain, string name, string samAccountName)
    {
        try
        {
            GroupPrincipal qbeGroup;
            using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain))
            {
                qbeGroup = new GroupPrincipal(ctx);
                qbeGroup.Name = !string.IsNullOrEmpty(name) ? name : "*";
                qbeGroup.SamAccountName = !string.IsNullOrEmpty(samAccountName) ? samAccountName : "*";
                PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
                ((DirectorySearcher)srch.GetUnderlyingSearcher()).PageSize = 500;

                List<AdGroup> listeGroupe = srch.FindAll()
                                      .OrderBy(x => x.SamAccountName)
                                      .Select(x => new AdGroup()
                                      {
                                          SamAccountName = x.SamAccountName,
                                          Description = x.Description,
                                          Domain = domain,
                                          NbMember = 0 //Can i Get a count of members in group here ?????
                                      })
                                      .ToList();
                return listeGroupe;
            }
        }
        catch (ArgumentNullException ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
        catch (Exception ex)
        {
            writeToLog(ex.Message, 1);
            return null;
        }
    }

public class AdGroup
    {
        public string SamAccountName { get; set; }
        public string Description { get; set; }
        public string Domain { get; set; }
        public int NbMember { get; set; }
    }

Thank you for the help

Richard


回答1:


One approach is to specify the type of the search result as GroupPrincipal using .OfType() after the call to FindAll(), and then you can get the members of each group as a collection using the Members collection property or the GetMembers() method, which has an optional boolean argument to specify if you need to search the group recursively for nested members. At that point, get the size of the collection.

List<AdGroup> listeGroupe = srch.FindAll()
    .OfType<GroupPrincipal>()
    .OrderBy(x => x.SamAccountName)
    .Select(x => new AdGroup()
    {
        SamAccountName = x.SamAccountName,
        Description = x.Description,
        Domain = domain,
        NbMember = x.Members.Count
    })
    .ToList();


来源:https://stackoverflow.com/questions/35089911/get-count-of-members-in-a-ad-group-using-principalsearcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!