.net FindByIdentity-性能差异

.net FindByIdentity-性能差异,.net,iis-7,directoryservices,.net,Iis 7,Directoryservices,下面的代码在我们域中的各种机器上运行良好 var context = new PrincipalContext(ContextType.Domain); var principal = UserPrincipal.FindByIdentity(context, @"domain\username") 但是,如果我在不在域上的机器上运行类似的代码,它可以工作,但FindByIdentity行需要2秒以上的时间 var context = new PrincipalContext(ContextT

下面的代码在我们域中的各种机器上运行良好

var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, @"domain\username")
但是,如果我在不在域上的机器上运行类似的代码,它可以工作,但FindByIdentity行需要2秒以上的时间

var context = new PrincipalContext(ContextType.Machine);
var principal = UserPrincipal.FindByIdentity(context, @"machinename\username")
能否通过向PrincipalContext构造函数和/或FindByIdentity方法提供特殊参数来解决此性能差异?IIS或Windows中是否有可以调整的设置?

至少,有人能告诉我为什么在第二种情况下会慢一些吗?


代码是从Windows Server 2008 R2上IIS 7.5(集成管道)中托管的ASP.NET MVC 3应用程序运行的。

我也遇到了同样的问题。请尝试下面的代码块。我不知道为什么,但它要快得多(忽略内置VS后第一次缓慢登录-后续登录速度很快)。见类似的SO问题


潜在的问题可能与netBios调用有关。请看

即使JimSTAT的答案很老,它还是引导我朝着正确的方向解决了我的问题。
我想指出,在我所有的Hyper-V-Switch网络接口上禁用TCP/IP上的NetBIOS可以消除我之前经历的每一次延迟。对
PrincipalContext()
UserPrincipal.FindByIdentity()
UserPrincipal.GetGroups()
的每次调用都会从远高于10000毫秒的时间减少到10毫秒或更低。在我看来,每个虚拟网卡都会被查询,这需要一个世纪的时间。幸运的是我找到了这个帖子

如果您熟悉Network Monitor或Wireshark,则可以捕获网络数据包,以查看每次执行此查询时与域控制器通话所需的时间。这可以给你一些提示。这只是active directory方面的问题,代码中没有任何错误。唯一奇怪的是,使用域控制器会更快。缓慢的环境只是工作组,所以它不应该接触任何东西。不过,我可以试试Wireshark,看看我是否发现了什么不寻常的东西。它也为我工作过。谢谢请注意,
PrincipalContext
UserPrincipal
以及
PrincipalSearcher
实现IDisposable。您应该在这里放置一些
usings()
。。。
var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;