使用Active Directory搜索进行有效分页 [英] Effective pagination with Active Directory searches

查看:130
本文介绍了使用Active Directory搜索进行有效分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中对Active Directory搜索进行分页的有效方法是什么?在广告中进行搜索的方法有很多,但到目前为止,我还找不到有效的方法。我希望能够指出跳过 Take 参数,并能够检索与我的搜索匹配的记录总数

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 want to be able to indicate Skip and Take parameters and be able to retrieve the total number of records matching my search criteria in the result.

我尝试使用 PrincipalSearcher 类:

I have tried searching with the PrincipalSearcher class:

using (var ctx = new PrincipalContext(ContextType.Domain, "FABRIKAM", "DC=fabrikam,DC=com"))
using (var criteria = new UserPrincipal(ctx))
{
    criteria.SamAccountName = "*foo*";

    using (var searcher = new PrincipalSearcher(criteria))
    {
        ((DirectorySearcher)searcher.GetUnderlyingSearcher()).SizeLimit = 3;
        var results = searcher.FindAll();
        foreach (var found in results)
        {
            Console.WriteLine(found.Name);
        }
    }
}

在这里我可以限制搜索结果为3,但无法获得与我的搜索条件相对应的记录总数( SamAccountName 包含 foo ),例如,我也无法向搜索者指示跳过前50条记录。

Here I was able to limit the search results to 3 but I wasn't able to get the total number of records corresponding to my search criteria (SamAccountName contains foo) neither I was able to indicate to the searcher to skip the first 50 records for example.

我也尝试使用System.DirectoryServices.DirectoryEntry 系统.DirectoryServices.Protocols.SearchRequest ,但我唯一能做的就是指定页面大小。

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.

推荐答案

您可以尝试使用虚拟列表视图搜索。以下按cn对用户进行排序,然后从第100位开始获得51个用户。

You may try the virtual list view search. The following sort the users by cn, and then get 51 users starting from the 100th one.

    DirectoryEntry rootEntry = new DirectoryEntry("LDAP://domain.com/dc=domain,dc=com", "user", "pwd");

    DirectorySearcher searcher = new DirectorySearcher(rootEntry);
    searcher.SearchScope = SearchScope.Subtree;
    searcher.Filter = "(&(objectCategory=person)(objectClass=user))";
    searcher.Sort = new SortOption("cn", SortDirection.Ascending);
    searcher.VirtualListView = new DirectoryVirtualListView(0, 50, 100);

    foreach (SearchResult result in searcher.FindAll())
    {
        Console.WriteLine(result.Path);
    }

对于您的用例,您只需要的BeforeCount,AfterCount和Offset属性为DirectoryVirtualListView(DirectoryVirtualListView ctor中的3)。 DirectoryVirtualListView的文档非常有限。您可能需要对它的行为进行一些实验。

For your use case you only need the BeforeCount, AfterCount and the Offset properties of DirectoryVirtualListView (the 3 in DirectoryVirtualListView ctor). The doc for DirectoryVirtualListView is very limited. You may need to do some experiments on how it behave.

这篇关于使用Active Directory搜索进行有效分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆