What would be an effective way to do pagination with Active Directory searches in .NET? There are many ways to search in AD but so far I couldn't find how to do it effectively.
I have tried following one
using (var ctx = new PrincipalContext(ContextType.Domain, "SAMPLE","DC=sample,DC=com"))
using (var criteria = new UserPrincipal(ctx))
{
criteria.SamAccountName = "*test*";
using (var searcher = new PrincipalSearcher(criteria))
{
((DirectorySearcher)searcher.GetUnderlyingSearcher()).SizeLimit = 10;
var results = searcher.FindAll();
foreach (var found in results)
{
}
}
}
Here I was able to limit the search results to 10 but I wasn't able to get the total number of records corresponding to my search criteria.
I also tried using the System.DirectoryServices.DirectoryEntry and System.DirectoryServices.Protocols.SearchRequest but the only thing I can do is specify the page size.
So is the only way to fetch all the results on the client and do the Skip and Count there? I really hope that there are more effective ways to achieve this directly on the domain controller.