我有这个代码:
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/