Reputation: 533
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.?
Upvotes: 6
Views: 12098
Reputation: 1
var LdapSearcher = new DirectorySearcher(RootDomain,
"(&(objectclass=user)" +
(!(string.IsNullOrEmpty(lastName.Trim())) ? "(sn=" + lastName.Trim() + ")" : "") +
(!(string.IsNullOrEmpty(firstName.Trim())) ? "(givenName=" + firstName.Trim() + ")" : "")
+ ")");
Upvotes: 0
Reputation: 3819
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 valueLet's say the user has provided the name "John Smith". Your filter should look like :
(&(objectClass=user)(|(sn=Smith)(givenName=John)))
Upvotes: 13