【问题标题】:Why would using PrincipalSearcher be faster than FindByIdentity()?为什么使用 PrincipalSearcher 比 FindByIdentity() 更快?
【发布时间】:2012-08-03 16:13:31
【问题描述】:

我有这个代码:

var context = new PrincipalContext( ContextType.Machine );
var user = UserPrincipal.FindByIdentity( context, username );

运行大约需要 2-3 秒。建议我使用PrincipalSearcher 类重写它:

var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;

它在不到一秒的时间内运行 - 明显更快。建议重写的人和我一样不知道为什么它运行得更快。

为什么它会产生任何性能差异?

【问题讨论】:

标签: c# .net security azure windows-server-2008


【解决方案1】:

我能想到的唯一合理的原因是.FindByIdentity 必须检查多个属性是否匹配,因为您没有具体指定要查找的属性。

您可以通过指定您要查找的属性来做到这一点 (using this method overload) - 试试这个进行比较:

var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

这有多快?

【讨论】:

  • 有趣,使用三参数 FindByIdentity() 的代码比原来的 FindByIdentity() 运行起来要慢一些。
  • 算法实现可能有差异?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 2011-05-01
  • 2023-01-30
  • 2011-05-29
  • 2018-08-19
  • 1970-01-01
相关资源
最近更新 更多