I have few issues when trying to get members from a group in LDAP AD.
1) I need page size of 1000, but it is returning 1500 members (Is there any issue from myside or need to ask admins??)
2) Cookie value is always null and I am not sure what is missing. Cookie is null and resultpagesize is null
Please help me if you came across this issue and solved this
Hashtable<String, Object> env = new Hashtable<String, Object>();
LdapContext ctx;
byte[] cookie = null;
try {
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost");
env.put(Context.SECURITY_PRINCIPAL,"cn=testaccount");
env.put(Context.SECURITY_CREDENTIALS, "passwd");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
ctx = new InitialLdapContext(env, null);
SearchControls searchCtls = new SearchControls();
String returnedAtts[]={"member"};
searchCtls.setSearchScope(2);
searchCtls.setReturningAttributes(returnedAtts);
ctx.setRequestControls(new Control[] { new PagedResultsControl(1000, false) });
do {
NamingEnumeration answer = ctx.search("", "(&(objectClass=group)(cn=testgroup))", searchCtls);
while (answer.hasMore()) {
SearchResult entry = (SearchResult) answer.next();
String attrsValaues = entry.getAttributes().toString();
System.out.println(attrsValaues);
}
Control[] controls = ctx.getResponseControls();
if (controls != null) {
for (int i = 0; i < controls.length; i++) {
System.out.println(controls[i]);
if (controls[i] instanceof PagedResultsResponseControl) {
PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
cookie = prrc.getCookie();
}
}
}
ctx.setRequestControls(new Control[] { new PagedResultsControl(1000, cookie, false) });
} while(cookie!=null);
} catch (Exception e) {
e.printStackTrace();
}
I found a way to fetch users without using cookie
boolean endString = true;
int loopValue = 0;
while (endString) {
int startValue = loopValue * 1000;
int endvalue = (loopValue + 1) * 1000;
SearchControls searchCtls = new SearchControls();
String[] returnedAttrs = new String[1];
String range = startValue + "-" + endvalue;
returnedAttrs[0] = "member;range=" + range;
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
searchCtls.setReturningAttributes(returnedAttrs);
NamingEnumeration answer = ctx.search("", "(&(objectClass=group)(cn=testgroup))", searchCtls);
while (answer.hasMore()) {
SearchResult entry = (SearchResult) answer.next();
System.out.println(entry.getAttributes());
if (entry.getAttributes().toString().contains("{member;range=" + startValue + "-*")) {
endString = false;
}
}
loopValue++;
}