[英]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.