Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Directory LDAP Search Filter or operator syntax

I am using LDAP Directory Services in C# to search users from LDAP with some filter criteria. I want to supply multiple OR filter criteria. For example firstName, lastName, telephone etc. It works fine when I supply all filter values but gives error when I just supply one or two filter values.

Here is the sample code I am using:

var LdapSearcher = new DirectorySearcher(RootDomain, 
                   "(&(objectclass=user)(sn=" + lastName.Trim() + ")(givenName=" + firstName.Trim() + "))");

I get the result when I supply both sn and givenName values. However, it's an OR search and user will enter either lastName or FirstName.

How to apply OR Filter in LDAP DirectorySearcher.?

like image 511
Pinal Dave Avatar asked Jun 08 '15 19:06

Pinal Dave


People also ask

What is LDAP syntax?

An attribute syntax is the LDAP equivalent of a data type. Every attribute type is associated (either explicitly or implicitly) with an attribute syntax, and all values for attributes of that type must abide by the constraints of that syntax.

What is LDAP search filter?

1. Search Filter is a basic LDAP Query for searching users based on mapping of username to a particular LDAP attribute. 2. The following are some commonly used Search Filters. You will need to use a search filter which uses the attributes specific to your LDAP environment.


1 Answers

You need to use the | operator. From what you've provided, your conditions are :

  • objectclass must be equal "user"
  • sn OR givenName must be equal to the provided value

Let's say the user has provided the name "John Smith". Your filter should look like :

(&(objectClass=user)(|(sn=Smith)(givenName=John)))

like image 113
X3074861X Avatar answered Sep 29 '22 08:09

X3074861X