python ldap3 reader.search_paged method returning 0 results - What am I doing wrong?

46 views Asked by At

I'm using the ldap3 library version 2.9.1 and python 3.10 on Windows 10. My search is working using the Reader.search method but when I switch to the Reader.search_paged, I get 0 results. Even if the query filter and such are exactly the same.

This works and returns 1000 rows:

obj_user = ObjectDef('user', conn_object)
obj_user += AttDef('sAMAccountName')

thesearch = '(sAMAccountName=a*)'
attributes = ['cn', 'distinguishedName']

r = Reader(conn_object, object_def=obj_user, base=bind_domain, query=thesearch)
r.search(attributes=attributes)
for currowindex, currow in enumerate(r):
     print('cn=', currow['cn'].value)

If I change out r.search for r.search_paged(paged_size=500) I get nothing.

2

There are 2 answers

1
Rick On

This situation appears to be a bug in the ldap3 library. After switching to the non-Reader search method, I was able to get paged content.

0
Cow On

I know you answered this yourself, I just want to share how we do it in our organization:

from ssl import CERT_REQUIRED
from ldap3 import Server, Connection, ALL, Tls


search_base: str = "OU=FOLDER_PATH,OU=IN_YOUR_AD,DC=AD_NAME,DC=AD_EXTENSION" # Where to start the search from
search_filter: str = "(sAMAccountName=a*)" # Your own example
attrs: list[str] = ['cn', 'distinguishedName'] # Your own example
username: str = "ad username"
password: str = "ad password"
server_uri: str = f"ldaps://server.name" # ldaps = LDAP Secure
tls_config: Tls = Tls(validate=CERT_REQUIRED) # if needed
server: Server = Server(server_uri, port=636, use_ssl=True, tls=tls_config, get_info=ALL) # this example features TLS over SSL
with Connection(server, username, password, auto_bind=True) as conn: # Create connection
    conn.start_tls() # Start TLS auth
    searchResultList: list = conn.extend.standard.paged_search(search_base, search_filter, attributes=attrs, paged_size=1000, generator=False)
    for result in searchResultList:
        ... # do whatever with result