问题
The following code works fine from a variety of machines on our domain.
var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, @"domain\username")
However, if I run this similar code on a machine that is not on a domain, it works but the FindByIdentity line takes 2+ seconds.
var context = new PrincipalContext(ContextType.Machine);
var principal = UserPrincipal.FindByIdentity(context, @"machinename\username")
Can this performance difference be addressed by supplying special parameters to the PrincipalContext constructor and/or the FindByIdentity method? Is there a setting in IIS or Windows that could be tweaked?
At the very least, can anybody tell me why it might be slower in the second scenario?
The code is running from an ASP.NET MVC 3 app hosted in IIS 7.5 (Integrated Pipeline) on Windows Server 2008 R2.
回答1:
I had the same problem. Try the below block of code. I don't know why but it's much faster (ignore first time slow login after build in VS - subsequent logins are speedy). See similar SO question Why would using PrincipalSearcher be faster than FindByIdentity()?
var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;
The underlying issue may have something to do with netBios calls. See ADLDS very slow (roundtrip to \Server*\MAILSLOT\NET\NETLOGON)
来源:https://stackoverflow.com/questions/7533790/findbyidentity-performance-differences