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 7619201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:37:34+00:00 2026-05-31T03:37:34+00:00

I got a working solution, however I’m pretty sure there is a less resource-intensive

  • 0

I got a working solution, however I’m pretty sure there is a less resource-intensive method because the current solution involves doing a query to get the groups member and then a query to get each users information.

Here is the code I have :

DirectoryEntry root = new DirectoryEntry( "LDAP://server:port" );
DirectorySearcher searcher = new DirectorySearcher( root );
searcher.Filter = "(&(ObjectClass=Group)(CN=foo-group))";

var members = (IEnumerable)searcher.FindOne()
              .GetDirectoryEntry()
              .Invoke( "members" );

Dictionary<string , string> results = new Dictionary<string , string>();

foreach( object member in members ) {
   DirectoryEntry de = new DirectoryEntry( member );
   results.Add( de.Properties[ "SAMAccountname" ][ 0 ].ToString(), de.Properties[ "cn" ][ 0 ].ToString() );
}

Ideally I’d like to be able to do a single query to get every user that are member of a group, filters the properties to load and then display them. So something like this

DirectoryEntry root = new DirectoryEntry( "LDAP://server:port" );
DirectorySearcher searcher = new DirectorySearcher( root );
searcher.PropertiesToLoad.Add( "cn" );
searcher.PropertiesToLoad.Add( "SAMAccountname" );
searcher.Filter = "(&(ObjectClass=user)(memberof=foo-group))";

foreach( var user in searcher.FindAll() ) {
    //do whatever...
}

Unfortunately, that doesn’t work for some reason.

  • 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-31T03:37:36+00:00Added an answer on May 31, 2026 at 3:37 am

    If you can use System.DirectoryServices.AccountManagement:

    var context = new PrincipalContext(ContextType.Domain, "YOUR_DOMAIN_NAME");
    using (var searcher = new PrincipalSearcher())
    {
        var groupName = "YourGroup";
        var sp = new GroupPrincipal(context, groupName);
        searcher.QueryFilter = sp;
        var group = searcher.FindOne() as GroupPrincipal;
    
        if (group == null)
            Console.WriteLine("Invalid Group Name: {0}", groupName);
    
        foreach (var f in group.GetMembers())
        {
            var principal = f as UserPrincipal;
    
            if (principal == null || string.IsNullOrEmpty(principal.Name))
                continue;
    
            Console.WriteLine("{0}", principal.Name);
        }
    }
    

    I have some VB code that’ll do it the old way also, but this is definitely simpler with AccountManagement.


    Here’s the VB code I was referring to (again it isn’t pretty but it’s functional):

    Public Function GetUsersByGroup(de As DirectoryEntry, groupName As String) As IEnumerable(Of DirectoryEntry)
        Dim userList As New List(Of DirectoryEntry)
        Dim group As DirectoryEntry = GetGroup(de, groupName)
    
        If group Is Nothing Then Return Nothing
    
        For Each user In GetUsers(de)
            If IsUserInGroup(user, group) Then
                userList.Add(user)
            End If
        Next
    
        Return userList
    End Function
    
    Public Function GetGroup(de As DirectoryEntry, groupName As String) As DirectoryEntry
        Dim deSearch As New DirectorySearcher(de)
    
        deSearch.Filter = "(&(objectClass=group)(SAMAccountName=" & groupName & "))"
    
        Dim result As SearchResult = deSearch.FindOne()
    
        If result Is Nothing Then
            Return Nothing
        End If
    
        Return result.GetDirectoryEntry()
    End Function
    
    Public Function GetUsers(de As DirectoryEntry) As IEnumerable(Of DirectoryEntry)
        Dim deSearch As New DirectorySearcher(de)
        Dim userList As New List(Of DirectoryEntry)
    
        deSearch.Filter = "(&(objectClass=person))"
    
        For Each user In deSearch.FindAll()
            userList.Add(user.GetDirectoryEntry())
        Next
    
        Return userList
    End Function
    
    Public Function IsUserInGroup(user As DirectoryEntry, group As DirectoryEntry) As Boolean
        Dim memberValues = user.Properties("memberOf")
    
        If memberValues Is Nothing OrElse memberValues.Count = 0 Then Return False
    
        For Each g In memberValues.Value
            If g = group.Properties("distinguishedName").Value.ToString() Then
                Return True
            End If
        Next
    
        Return False
    End Function
    

    And usage:

    Dim entries = New DirectoryEntry("LDAP://...")
    Dim userList As IEnumerable(Of DirectoryEntry) = GetUsersByGroup(entries, "GroupName")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: We are still using XP at work and I got my solution working,
I installed TeamCity and got it working against my project. However, I have since
I just got done working through the Django tutorials for the second time, and
I've got a working SOAP::Lite client. It works against an established server, but so
Has anyone got this working in a web application? No matter what I do
I've got Drupal working on a shared host, and I uploaded some modules from
I've got ELMAH working on my (Cassini) development server, and was quite happy with
This unix command I haven't got quite working on Mac yet - any ideas
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
Update: Solved, with code I got it working, see my answer below for the

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.