![](/img/trans.png)
[英]What are the different properties available in System.DirectoryServices.DirectorySearcher.PropertiesToLoad
[英]System.DirectoryServices.DirectorySearcher caching improvement or FindAll() filtering
我正在嘗試提高對活動目錄分層組結構的 searcher.FindAll() 調用的性能。
該代碼獲取所有用戶組的 SearchResultCollection,然后根據搜索字符串過濾組。 SearchResultCollection 集合中有大約 400 個組,過濾后的版本通常有 40 個。我認為在迭代調用期間內置緩存沒有任何好處 (DirectorySearcher.CacheResults = True)。
例如,如果我連續調用以下代碼 20 次,執行時間幾乎相同。
下面的代碼在每次執行時對 SearchResult 的所有 400 次迭代都會收到相同的懲罰。
string groupFilter = "ExampleFilter";
DirectoryEntry root = GetRootDirectoryEntry();
string userPath = GetUserPath(username, root);
var searcher = new DirectorySearcher(root)
{
SearchScope = SearchScope.Subtree,
Filter = string.Format("(member:XXXX:={0})", userPath) + ""
//Any wildcard based search for distinguishedname like below returns no results, which is incorrect
//Filter = string.Format("(& (member:XXXX:={0}) (distinguishedname=*{1}) )", userPath, groupFilter)
};
searcher.PropertiesToLoad.Add("Name");
searcher.PropertiesToLoad.Add("distinguishedname");
var groupResults = searcher.FindAll();
var allFilteredGroups = new HashSet<string>();
foreach (SearchResult gr in groupResults) //approx 400 in the groupResults, same time penalty for every iteration, every time
{
var isRelevant = gr.Path.Contains(groupFilter);
if (isRelevant)
{
//Do Stuff
allFilteredGroups.Add(value);
}
}
return allFilteredGroups.ToList(); //approx 40
為什么緩存沒有提供任何改進? 關於如何減少迭代所有這些我知道我不想要的組的懲罰的任何建議?
我假設您試圖只包含來自特定 OU 的組?
您無法通過部分distinguishedName
過濾是正確的。 但是,您可以通過修改傳遞給DirectorySearcher
的SearchRoot
來僅在一個特定 OU 中進行搜索。 例如:
DirectoryEntry root = new DirectoryEntry("LDAP://OU=MyGroups,DC=example,DC=com");
該DirectoryEntry
指向MyGroups
OU。 因此,如果您將其傳遞給DirectorySearcher
,則它只會在該 OU 中進行搜索。
您還可以設置SearchScope
到OneLevel
,如果你不希望它來搜索子OU。 Subtree
是默認的,所以如果這是你想要的,你根本不需要設置它。
即使您想在多個 OU 中查找組,您最好重復搜索您想要的每個 OU,而不是要求所有內容並丟棄大部分結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.