我有这样的代码:
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;而且它的运行时间不到一秒--明显更快。为什么建议重写的人和我一样不知道为什么它运行得更快。
为什么它会造成性能上的差异?
发布于 2012-08-04 00:23:14
我能想到的唯一合理的原因是,.FindByIdentity必须检查多个属性是否匹配,因为您没有确切指定要查找的属性。
您可以通过指定要查找的属性(using this method overload)来完成此操作-尝试执行以下操作进行比较:
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);这有多快?
https://stackoverflow.com/questions/11799457
复制相似问题