简体   繁体   中英

Why would using PrincipalSearcher be faster than FindByIdentity()?

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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM