none
Get all Groups with all users from ActiveDirectory RRS feed

  • Question

  • Hi,

    Im trying to query the ad to get all groups and all members in the groups. so far I can use my function to get gruops by string username, thats okay for me, except that I need that for all Users in my Domain. 

    string[] output = null;
    
                string username = "username";
    
                using (var ctx = new PrincipalContext(ContextType.Domain))
                using (var user = UserPrincipal.FindByIdentity(ctx, username))
                {
                    if (user != null)
                    {
                        output = user.GetGroups() //this returns a collection of principal objects
                            .Select(x => x.Name) // select the Sid.  you may change this to choose the display name or whatever you want
                            .ToArray(); // convert to string array
    
                        foreach (var res in output)
                        {
    
                            try
                            {
                                Console.WriteLine("Group" + res + " has Member " + username + ");
                            }
                            catch
                            {
                                Console.WriteLine("Group and Person relation not found");
                                Console.WriteLine();
                                await session.CloseAsync();
                            }
    
                        };
                        await driver.CloseAsync();
    
                    }
                }

    I think I have to start with something like:

    using (PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context)))

                        foreach (var result in searcher.FindAll())
                        {
                            DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;

    username = de.[samAccountName].value;

    }


    but then im lost...

    Hope someone could help me to resolve that.

    Thursday, February 6, 2020 12:15 PM

All replies

  • To get all users in the domain you'll have to enumerate the users on the domain directly.

    Note that user.GetGroups only returns the groups that the user is a direct member of. If your domain has groups within groups then parent groups won't show up. For example:

    MyUsers
       MySpecialUsers
          UserA

    This structure would only return MySpecialusers. To get all users use GetAuthorizationGroups.



    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 6, 2020 2:52 PM
    Moderator
  • Hi CodeCase,

    Thank you for posting here.

    There are many answers to this question on stackoverflow, check out the following links, it may be helpful to you.

    How to: (Almost) Everything In Active Directory via C#

    How to get all the AD groups for a particular user?

    How to get the groups of a user in Active Directory? (c#, asp.net)

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 7, 2020 3:29 AM
  • Hi CodeCase,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 8:40 AM