DirectorySearcher doesn't respect the SizeLimit property if the SizeLimit is 256 or over

662 views Asked by At

I'm using PrincipalSearcher and DirectorySearcher to filter users and specify which properties to grab. On testing it on 2 different networks. One network has 8K+ records and the other has 135K+. On the 135K+ network, it fails to save the records to a database. So I want to do a test with a handful of records, but it doesn't fail on the couple hundred of records I've tried. So I tried increasing it to a thousand. But when I do that, it gives me everything instead of the thousand I requested.

Here's how I'm doing this:

using (PrincipalSearcher searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
    DirectorySearcher directorySearcher = searcher.GetUnderlyingSearcher() as DirectorySearcher;
    directorySearcher.Filter = "(&(objectSid=*)(sn=*)(givenName=*)(objectGuid=*))";
    directorySearcher.PropertiesToLoad.Add("ObjectGUID");
    directorySearcher.PropertiesToLoad.Add("objectSid");
    directorySearcher.PropertiesToLoad.Add("GivenName");
    directorySearcher.PropertiesToLoad.Add("sn");
    directorySearcher.PropertiesToLoad.Add("mail");
    directorySearcher.PropertiesToLoad.Add("telephoneNumber");
    directorySearcher.PropertiesToLoad.Add("sAMAccountName");
    directorySearcher.PropertiesToLoad.Add("distinguishedName");
    directorySearcher.PropertiesToLoad.Add("title");
    directorySearcher.PropertiesToLoad.Add("department");
    directorySearcher.PropertiesToLoad.Add("company");
    directorySearcher.PropertiesToLoad.Add("manager");
    directorySearcher.PropertiesToLoad.Add("PhysicalDeliveryOfficeName");
    directorySearcher.PropertiesToLoad.Add("countryCode");
    directorySearcher.SizeLimit = 255;

Notice the size limit here is 255. If I specify a value under 256, it will return the number of I records I specify. Quick example of it in action on the small network:

enter image description here

If I tell it to give me 255 records, it gives me 255 records. But if I tell it to give me 256 or higher, it wants to give me all of them.

enter image description here

Any ideas on what's going on here?

1

There are 1 answers

0
ernest On BEST ANSWER

The problem was the PageSize property on the DirectorySearcher object. It seems like the page size was limited when a non-default value was set to the SizeLimit property. I set the PageSize property to 4000 and any value I put in the SizeLimit pulled that exact number.