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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With