Effective pagination with Active Directory searches

前端 未结 4 1185
广开言路
广开言路 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:20

    Waaaay late to the party, but this is what I'm doing:

    I'm using FindOne() instead of FindAll() and member;range=<start>-<end> on PropertiesToLoad.

    There's a catch on member;range: when it's the last page, even if you pass member;range=1000-1999 (for instance), it returns member;range=1000-*, so you have to check for the * at the end to know if there is more data.

    public void List<string> PagedSearch()
    { 
        var list = new List<string>();
        bool lastPage = false;
        int start = 0, end = 0, step = 1000;
    
        var rootEntry = new DirectoryEntry("LDAP://domain.com/dc=domain,dc=com", "user", "pwd");
    
        var filter = "(&(objectCategory=person)(objectClass=user)(samAccountName=*foo*))";
    
        using (var memberSearcher = new DirectorySearcher(rootEntry, filter, null, SearchScope.Base))
        {
            while (!lastPage)
            {
                start = end;
                end = start + step - 1;
    
                memberSearcher.PropertiesToLoad.Clear();
                memberSearcher.PropertiesToLoad.Add(string.Format("member;range={0}-{1}", start, end));
    
                var memberResult = memberSearcher.FindOne();
    
                var membersProperty = memberResult.Properties.PropertyNames.Cast<string>().FirstOrDefault(p => p.StartsWith("member;range="));
    
                if (membersProperty != null)
                {
                    lastPage = membersProperty.EndsWith("-*");
                    list.AddRange(memberResult.Properties[membersProperty].Cast<string>());
                    end = list.Count;
                }
                else
                {
                    lastPage = true;
                }
            }
        }
        return list;
    }
    
    0 讨论(0)
  • 2021-02-05 06:23
        private static DirectoryEntry forestlocal = new DirectoryEntry(LocalGCUri, LocalGCUsername, LocalGCPassword);
        private DirectorySearcher localSearcher = new DirectorySearcher(forestlocal);
    
         public List<string> GetAllUsers() 
        {
            List<string> users = new List<string>();
    
            localSearcher.SizeLimit = 10000;
            localSearcher.PageSize = 250;
    
            string localFilter = string.Format(@"(&(objectClass=user)(objectCategory=person)(!(objectClass=contact))(msRTCSIP-PrimaryUserAddress=*))");
    
            localSearcher.Filter = localFilter;
    
            SearchResultCollection localForestResult;
    
            try
            {
                localForestResult = localSearcher.FindAll();
    
                if (resourceForestResult != null) 
                {
    
                    foreach (SearchResult result in localForestResult) 
                    {
                        if (result.Properties.Contains("mail"))
                            users.Add((string)result.Properties["mail"][0]);
                    }
    
                }
    
            }
            catch (Exception ex) 
            {
    
            }
    
            return users;
        }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 06:29

    If SizeLimit is set to zero and PageSize is set to 500, the search will return all 12,000 results in pages of 500 items, with the last page containing only 200 items. The paging occurs transparently to the application and the application does not have to perform any special processing other than setting the PageSize property to the proper value.

    SizeLimit limits the amount of results that you can retrieve at once - so your PageSize needs to be less than or equal to 1000 (Active Directory limits the maximum number of search results to 1000. In this case, setting the SizeLimit property to a value greater than 1000 has no effect.). The paging is done automagically behind the scenes when you call FindAll() etc.

    For more details please refer MSDN

    https://msdn.microsoft.com/en-us/library/ms180880.aspx

    https://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.pagesize.aspx

    https://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit.aspx

    0 讨论(0)
提交回复
热议问题