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.
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)
Even with JimSTAT's answer being quite old, it led me into the right direction solving my problem.
I would like to point out that disabling NetBIOS over TCP/IP
on all my Hyper-V-Switch network interfaces eliminated every delay I had experienced beforehand. Every call to PrincipalContext()
, UserPrincipal.FindByIdentity()
and UserPrincipal.GetGroups()
is down to 10 ms or lower, from well above 10,000 ms. It appears to me that every virtual network card was being queried which took a felt century to time out. Lucky me I found this post!
精彩评论