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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:12:17+00:00 2026-05-12T23:12:17+00:00

I am attempting to query ActiveDirectory via LDAP, but the query may contain spaces

  • 0

I am attempting to query ActiveDirectory via LDAP, but the query may contain spaces or other characters that may cause problems (hyphens?)

(&(objectCategory=person)(objectClass=user)(|(&(sn=Bloggs*)(givenName=Jo*))(displayName=Jo Bloggs))

It is an OR search e.g. in SQL it would be WHERE (sn LIKE 'Bloggs%' AND givenName LIKE 'Jo%') OR displayName = 'Jo Bloggs'

However, when I try the LDAP query, I get an error: System.ArgumentException: The (&(objectCategory=person)(objectClass=user)(|(&(sn=Bloggs*)(givenName=Jo*))(displayName=Jo Bloggs)) search filter is invalid

Code for performing search:

string userName = "Jo Bloggs";
DirectoryEntry adroot = new DirectoryEntry("LDAP://" + Environment.UserDomainName, "user", "password", AuthenticationTypes.Secure);
DirectorySearcher search = new DirectorySearcher(adroot);
search.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(|(&(sn={0}*)(givenName={1}*))(displayName={2}))", userName.Split(' ')[1], userName.Split(' ')[0], userName);

This is just a basic search, I would like to search other columns as well (Job Title, Telephone, Department etc), e.g. WHERE title LIKE '%foo%' OR telephonenumber LIKE '%foo% OR department LIKE '%foo%'

Also, could I cache the search, so ActiveDirectory doesn’t get a lot of hits from people searching t for the same thing?

This also only finds one entry, I would like to search and display in a repeater all results that are found.

  • 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-12T23:12:18+00:00Added an answer on May 12, 2026 at 11:12 pm

    You’re missing a closing parenthesis. Try this working example:

    string userName = "Jo Bloggs";
    string baseQuery =
        "(&" +
            "(objectCategory=person)" +
            "(objectClass=user)" +
            "(|" +
                "(&" +
                    "(sn={0}*)" +
                    "(givenName={1}*)" +
                ")" +
                "(displayName={2})" +
            ")" +
        ")"; // <<< this is missing in your original query
    
    userName = Regex.Replace(userName, @"[\(\)\*\\]", (match) =>
                    {   // escape reserved chars
                        return "\\" + ((int)match.Value[0]).ToString("x");
                    }, RegexOptions.Compiled);
    string query = String.Format(query, userName.Split(' ')[1], 
                                        userName.Split(' ')[0], userName);
    using (DirectoryEntry entry = new DirectoryEntry(
        "LDAP://" + Environment.UserDomainName, "user", "password",
        AuthenticationTypes.Secure))
    {
        using (DirectorySearcher ds = 
           new DirectorySearcher(entry, query, null, SearchScope.Subtree))
        {
            SearchResultCollection res = ds.FindAll(); // all matches
            if (res != null)
                foreach (SearchResult r in res)
                    Console.WriteLine(user.Properties["displayName"].Value);
        }
    }
    

    EDIT: About escape sequences, you should refer to this document: Creating a Query Filter. I edit this answer to reflect that information.

    If any of the following special characters must appear in the query filter as literals, they must be replaced by the listed escape sequence.

      ASCII     Escape sequence 
    character     substitute
        *           "\2a"
        (           "\28"
        )           "\29"
        \           "\5c"
       NUL          "\00"
    

    In addition, arbitrary binary data may be represented using the escape sequence syntax by encoding each byte of binary data with the backslash followed by two hexadecimal digits. For example, the four-byte value 0x00000004 is encoded as “\00\00\00\04” in a filter string.

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

Sidebar

Related Questions

I am attempting to return a single object via castor query that has the
The database table only contains the four fields that the query is attempting to
I'm attempting to query AD in an ASP.Net (4.0) application that is running on
I'm attempting to mangle a SQL query via regex. My goal is essentially grab
I have a Premiums table that I attempting to query using the following LINQ-to-SQL:
I am attempting to run a sql query but get the following error: Incorrect
I'm attempting to make a query that lists a count of Active, Inactive &
I'm attempting to modify a mySQL query (that works) to return a more specific
I'm attempting to construct a LIKE operator in my query on DB2 that is
I am attempting to execute the following query via the mysqldb module in python:

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.