[英]Why would using PrincipalSearcher be faster than FindByIdentity()?
我有以下代码:
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
必须检查多个属性是否匹配,因为您没有确切指定要查找的属性。
您可以通过指定要查找的属性来做到这一点( 使用此方法重载 )-尝试进行比较:
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);
这有多快?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.