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

  • Home
  • SEARCH
  • 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 542167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:25:07+00:00 2026-05-13T10:25:07+00:00

I have been banging my head for quite a while with this and can’t

  • 0

I have been banging my head for quite a while with this and can’t get it to work. I have a LDAP Query I do have working in AD Users and Computers but dont know how to do it programatically in C#.

Here are my LDAP Query that works fine in the AD Tool: (memberOf=CN=AccRght,OU=Groups,OU=P,OU=Server,DC=mydomain,DC=com)(objectCategory=user)(objectClass=user)(l=City)

I have used this code to get the user accounts to get members of CN=AccRght but I’m not succeeding on limiting users belonging to a specific city.

public StringCollection GetGroupMembers(string strDomain, string strGroup)
{
    StringCollection groupMemebers = new StringCollection();
    try
    {
        DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
        DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
        SearchResultCollection coll = srch.FindAll();
        foreach (SearchResult rs in coll)
        {
            ResultPropertyCollection resultPropColl = rs.Properties;
            foreach( Object memberColl in resultPropColl["member"])
            {
                DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                object obVal = userProps["sAMAccountName"].Value;
                if (null != obVal)
                {
                    groupMemebers.Add(obVal.ToString());
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message);
    }
    return groupMemebers;
}

Thanks for any help!

  • 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-13T10:25:07+00:00Added an answer on May 13, 2026 at 10:25 am

    Well, basically all you need is to transfer that LDAP filter you’re using in the tool into your DirectorySearcher – something like this:

    public StringCollection GetGroupMembers(string strDomain, string strGroup)
    {
        StringCollection groupMemebers = new StringCollection();
    
        try
        {
            DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
    
            DirectorySearcher srch = new DirectorySearcher();
    
            // build the LDAP filter from your (CN=strGroup) part that you had
            // in the constructor, plus that filter you used in the AD tool
            // to "AND" those together, use the LDAP filter syntax:
            //  (&(condition1)(condition2))  
            srch.Filter = string.Format("(&(CN={0})(memberOf=CN=AccRght,OU=Groups,OU=P,OU=Server,DC=mydomain,DC=com)(objectCategory=user)(objectClass=user)(l=City))", strGroup);
    
            SearchResultCollection coll = srch.FindAll();
    
            foreach (SearchResult rs in coll)
            {
                ResultPropertyCollection resultPropColl = rs.Properties;
    
                foreach( Object memberColl in resultPropColl["member"])
                {
                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                    object obVal = userProps["sAMAccountName"].Value;
                    if (null != obVal)
                    {
                        groupMemebers.Add(obVal.ToString());
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return groupMemebers;
    }
    

    That should apply that filter to your search, e.g. you should now only get back users for that specific city.

    Definitely check out this MSDN article Managing Directory Security Principals in the .NET Framework 3.5 – excellent intro to S.DS.AM ! 🙂

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

Sidebar

Related Questions

I have been banging my head over this one for quite a while now
Been banging my head on this for a while now The problem I have
Ok, I have been banging my head on this one for a while, figure
I have been banging my head against the wall- can anyone help with working
I have been banging my head against this for a little while now. I
I have been banging my head against this one for a while now and
I have been banging my head against the wall with this odd behaviour on
I have been banging my head on a wall with this one. I need
I have been banging my head on this (im a newbie in MySQL, be
I have been banging my head on this problem for sometime. There are some

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.