Alex Rivera | Logout

Why would using PrincipalSearcher be faster than FindByIdentity()?

Asked 2012-08-03T16:13:31.773
16

I had this code:

var context = new PrincipalContext( ContextType.Machine );
var user = UserPrincipal.FindByIdentity( context, username );

and it took about 2-3 seconds to run. I was recommended to rewrite it using PrincipalSearcher class:

var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;

and it runs in less than one second - notably faster. The person why advised the rewrite is as clueless as me why it runs faster.

Why does it make any performance difference?

Edit
Report

1 Answer

6

The only plausible reason I can think of is that .FindByIdentity has to check multiple attributes for a match, since you're not specifying exactly which attribute you're looking for.

You can do that by specifying the attribute you're looking for (using this method overload) - try this for a comparison:

var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

How fast is this?

answered 2012-08-03T16:23:13.807

Your Answer