Effective pagination with Active Directory searches

前端 未结 4 1179
广开言路
广开言路 2021-02-05 06:00

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.

4条回答
  •  后悔当初
    2021-02-05 06:25

    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);
        }
    

    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.

提交回复
热议问题