public void PagedSearch(QueryDTO qdto, int pageSize, IntPtr cookie, bool morePages, Action <ILdapMessage, IntPtr, bool, List <ILdapEntry> > fn) { ILdapMessage ldMsg = null; IntPtr[] serverControls = { IntPtr.Zero }; IntPtr returnedControls = IntPtr.Zero; MaintainSession(delegate() { serverControls[0] = ldConn.LdapCreatePageControl(pageSize, cookie, true); ldMsg = ldConn.LdapSearchExtExS(qdto.SearchBase, (int)qdto.SearchScope, qdto.GetFilterString(), qdto.AttrToReturn, qdto.AttrOnly, qdto.TimeOut, qdto.SizeLimit, serverControls); ldConn.LdapParseResult(ldMsg, ref returnedControls, false); if (cookie != IntPtr.Zero) { LdapClientLibrary.ber_bvfree(cookie); cookie = IntPtr.Zero; } ldConn.LdapParsePageControl(returnedControls, ref cookie); morePages = ldConn.HasMorePages(cookie); if (returnedControls != IntPtr.Zero) { LdapClientLibrary.ldap_controls_free(returnedControls); returnedControls = IntPtr.Zero; } LdapClientLibrary.ldap_control_free(serverControls[0]); serverControls[0] = IntPtr.Zero; }); List <ILdapEntry> entries = ldMsg.GetEntries(); if (fn != null) { fn(ldMsg, cookie, morePages, entries); } (ldMsg as LdapMessage).FreeMessage(); }
public static void LdapPagedSearch() { ILdapConnection ldapConnection = LdapConnection.LdapInit(hostName, portNumber); Assert.IsNotNull(ldapConnection); ldapConnection.LdapSimpleBindS(myDN, password); int totalCount = 0; int pageNumber = 1; bool morePages = false; int pageSize = 2; IntPtr cookie = IntPtr.Zero; IntPtr returnedControls = IntPtr.Zero; IntPtr[] serverControls = { IntPtr.Zero }; ILdapMessage msg = null; string searchBase = "cn=users,dc=vsphere,dc=local"; string filter = "(objectClass=user)"; System.Console.WriteLine("The entries returned were:"); do { serverControls[0] = ldapConnection.LdapCreatePageControl(pageSize, cookie, true); msg = ldapConnection.LdapSearchExtExS(searchBase, (int)LdapScope.SCOPE_SUBTREE, filter, null, 0, IntPtr.Zero, 0, serverControls); ldapConnection.LdapParseResult(msg, ref returnedControls, false); if (cookie != IntPtr.Zero) { LdapClientLibrary.ber_bvfree(cookie); cookie = IntPtr.Zero; } ldapConnection.LdapParsePageControl(returnedControls, ref cookie); morePages = ldapConnection.HasMorePages(cookie); if (returnedControls != IntPtr.Zero) { LdapClientLibrary.ldap_controls_free(returnedControls); returnedControls = IntPtr.Zero; } LdapClientLibrary.ldap_control_free(serverControls[0]); serverControls[0] = IntPtr.Zero; if (morePages) { System.Console.WriteLine("===== Page : {0} =====", pageNumber); } totalCount += msg.GetEntriesCount(); foreach (var entry in msg.GetEntries()) { System.Console.WriteLine("dn: {0}", entry.getDN()); } ldapConnection.CleanSearch(); pageNumber++; } while (morePages); System.Console.WriteLine("{0} entries found during the search", totalCount); LdapClientLibrary.ber_bvfree(cookie); cookie = IntPtr.Zero; ldapConnection.LdapUnbindS(); }