1

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

Richard
  • 27
  • 1
  • 6

1 Answers1

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();
Chase
  • 934
  • 6
  • 18
  • Thank you, I did try your suggestion but I get this Exception "While trying to resolve a cross-store reference, the target principal could not be found in the domain indicated by the principal's SID." : Is this indicate that a member of the group might not be part of the domain from the actual context ? – Richard Jan 29 '16 at 18:33
  • Yes, that error suggests that a member of one of your groups on your domain has an invalid SID. – Chase Jan 30 '16 at 00:09