c# - 为什么使用 PrimarySearcher 比 FindByIdentity() 更快?

标签 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;

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

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

最佳答案

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

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

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

这有多快?

关于c# - 为什么使用 PrimarySearcher 比 FindByIdentity() 更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11799457/

相关文章:

c# - 为什么在计算表达式时会得到错误的结果?

c# - 通过 EF Core 将 1.3GB CSV 文件导入 sqlite

c# - 泛型和 Action 委托(delegate)

asp.net - 从请求中识别应用程序池的名称

php - 由当前登录的用户安全更新或插入行 'owned'

security - session 和基于 token 的身份验证之间的技术差异

c# - 如何实现类似 "Fences"的东西

c# - 初始化的对象在 OnAwake 和 OnEnable() 之间变为 null

c# - "' 表格1 ' already defines a member called ' .ctor ' with the same parameter types "

security - 经典 ASP —— 防止 HTTP header 注入(inject)