I have the following method which searches for users in AD:
private void SearchUsers(string firstName, string lastName)
{
using (var context = new PrincipalContext(ContextType.Domain, request.DomainName))
{
var up = new UserPrincipal(context);
var ps = new PrincipalSearcher(up);
var userSearchResults = ps.FindAll().ToList();
if (!string.IsNullOrEmpty(firstName))
userSearchResults = userSearchResults.Where(
x => x.GivenName?.ToUpper() == firstName.ToUpper()).ToList();
if (!string.IsNullOrEmpty(lastName))
userSearchResults = userSearchResults.Where(
x => x.Surname?.ToUpper() == lastName.ToUpper()).ToList();
}
}
Is there a more efficient way to search as opposed to the way in which the search is being done in this method? Could I use AsQueryable here?