为什么使用PrincipalSearcher比FindByIdentity()更快?

时间:2012-08-03 16:13:32

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

我有这段代码:

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;

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

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

1 个答案:

答案 0 :(得分:5)

我能想到的唯一可信的原因是.FindByIdentity必须检查匹配的多个属性,因为您没有准确指定您正在寻找的属性。

您可以通过指定要查找的属性(using this method overload)来执行此操作 - 尝试进行比较:

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

这有多快?

相关问题