Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3360362
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:59:00+00:00 2026-05-18T02:59:00+00:00

In my Active Directory (my.domain), I have many groups (UserGrp1, UserGrp2, etc.) which have

  • 0

In my Active Directory (my.domain), I have many groups (UserGrp1, UserGrp2, etc.) which have many users. A user can exist in more than one group. I currently have code that allows me to use the GroupPrincipal class to find a group, and then from there to get all members of that group (see code below). However, what I really need is to find all groups to which a user belongs. For instance, I have a domain user named Joe Test (sAMAccountName=JOETEST) and I need to find all groups to which he belongs. What is the best way to do this?

I can determine if a user belongs to a group (as below) if I loop through all members returned by the GetMembers() method, but this seems inefficient to me and I’d be surprised were there not a more efficient way.

using (PrincipalContext ctx = new PrincipalContext(
  ContextType.Domain, "my.domain", "DC=my,DC=domain")) {

  if (ctx != null) {
    using (GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, "UserGrp1")) {
      // Get all group members
      PrincipalSearchResult<Principal> psr = gp.GetMembers();
      foreach (Principal p in psr) {
         // other logic 
      }
    }
  }
}

Thanks in advance for any help that I receive on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:59:00+00:00Added an answer on May 18, 2026 at 2:59 am

    Do it by using UserPrincipal.GetGroups();

    For a complete code here it is

    /// <summary>
    /// Gets a list of the users group memberships
    /// </summary>
    /// <param name="sUserName">The user you want to get the group memberships</param>
    /// <returns>Returns an arraylist of group memberships</returns>
    public ArrayList GetUserGroups(string sUserName)
    {
        ArrayList myItems = new ArrayList();
        UserPrincipal oUserPrincipal = GetUser(sUserName);
    
        PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
    
        foreach (Principal oResult in oPrincipalSearchResult)
        {
            myItems.Add(oResult.Name);
        }
        return myItems;
    }
    
    
    
    /// <summary>
    /// Gets a certain user on Active Directory
    /// </summary>
    /// <param name="sUserName">The username to get</param>
    /// <returns>Returns the UserPrincipal Object</returns>
    public UserPrincipal GetUser(string sUserName)
    {
        PrincipalContext oPrincipalContext = GetPrincipalContext();
    
        UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
        return oUserPrincipal;
    }
    
    
    /// <summary>
    /// Gets the base principal context
    /// </summary>
    /// <returns>Retruns the PrincipalContext object</returns>
    public PrincipalContext GetPrincipalContext()
    {
        PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.SimpleBind, sServiceUser, sServicePassword);
        return oPrincipalContext;
    }
    

    or for a full AD reference go here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Active Directory domains, A and B. Users in domain A need
I have managed to query my Active Directory Domain Controller for user info using
We have ColdFusion and Active Directory running within the same domain. I'm trying to
I have an application that pulls names from the active directory for the domain
I have an ASP.NET web site that will use Active Directory to store Users.
Within our Active Directory domain, we have a MS SQL 2005 server, and a
I am using C# application to authenticate and check Active Directory user which locates
I can connect to a domain's Active Directory using ADsGetObject : IADs domain; ADsGetObject(LDAP://fabrikam.com,
I created domain user 'jsmith' in Active Directory and i've added that domain account
I have to check usergroups of LDAP Active Directory for a specific user in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.