Search Users in Active Directory based on First Name, Last Name and Display Name

4.7k views Asked by At

I trying to search my organization Active directory for users.

If the FirstName or LastName or DisplayName matches a particular string value, it should return the users.

My Code:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Ramesh*";
// qbeUser.Surname = "Ramesh*";
// qbeUser.DisplayName= "Ramesh*";    

PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    //
}

The problem is that I am able to search by only one filter.

I am able to AND the filters but not OR. Whether any solutions are available?

2

There are 2 answers

1
marc_s On BEST ANSWER

See a possible solution for this issue in this other SO question.

You will need to use the extensibility of UserPrincipal to create a descendant class, in order to get access to the anr property (anr = ambiguous name resolution) which allows searches in multiple name-related properties at once.

0
KMarron On

Have a look at the DirectorySearcher. This article may help.