I'm rewriting my C# program to Java and became very curios about the fact that C# application can extract tens of thousands of users with this trick:
DirectorySearcher search = new DirectorySearcher(entry);
search.SizeLimit = 99000;
search.PageSize = 98000;
but my Java programs firmly say
LDAPSearchException(resultCode=4 (size limit exceeded), numEntries=1000, numReferences=0, errorMessage='size limit exceeded')
I tried both unboundid and standard libraries. Found million discussions about this problem and everywhere is said - the limitation is on server, you can do nothing.
But my C# application does work! How can this happen? Secret techniques from Microsoft, that cannot be repeated by other vendors?
Just in case, my code is:
SearchRequest searchRequest = new SearchRequest(path, SearchScope.SUB, filter, "SamAccountName");
searchRequest.setSizeLimit(99000);
searchRequest.setTimeLimitSeconds(999);
SearchResult result = connection.search(searchRequest);
for (SearchResultEntry sre : result.getSearchEntries()) {
System.out.println(count++ + ": " + sre.toString());
}
for unboundid
p.s. I do not want to use workaround with searching for a*, b*, c*
et c. Especially, considering that usernames might be not only in English.
Further reading showed, that unboundid does support paged mode, so problem is solved.
public static void main(String[] args) {
try {
int count = 0;
LDAPConnection connection = new LDAPConnection("hostname", 389, "user@domain", "password");
final String path = "OU=Users,DC=org,DC=com";
String[] attributes = {"SamAccountName","name"};
SearchRequest searchRequest = new SearchRequest(path, SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person"), attributes);
ASN1OctetString resumeCookie = null;
while (true)
{
searchRequest.setControls(
new SimplePagedResultsControl(100, resumeCookie));
SearchResult searchResult = connection.search(searchRequest);
for (SearchResultEntry e : searchResult.getSearchEntries())
{
if (e.hasAttribute("SamAccountName"))
System.out.print(count++ + ": " + e.getAttributeValue("SamAccountName"));
if (e.hasAttribute("name"))
System.out.println("->" + e.getAttributeValue("name"));
}
LDAPTestUtils.assertHasControl(searchResult,
SimplePagedResultsControl.PAGED_RESULTS_OID);
SimplePagedResultsControl responseControl =
SimplePagedResultsControl.get(searchResult);
if (responseControl.moreResultsToReturn())
{
resumeCookie = responseControl.getCookie();
}
else
{
break;
}
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}