I'm using library ldap to connect to active directory.
import logging
import ldap
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('my_logger')
try:
ldap_connect = ldap.initialize(host)
ldap_connect.protocol_version = ldap.VERSION3
ldap_connect.set_option(ldap.OPT_REFERRALS, 0)
logger.info('successful connection')
ldap_connect.unbind()
except Exception as e:
logger.error(f'Error: {str(e)}')
try:
ldap_connect.simple_bind_s(user, password)
logger.info('successful bind LDAP')
except ldap.INVALID_CREDENTIALS as e:
logger.warning('Error bind')
except Exception as e:
logger.error(f'Error: {str(e)}')
Connection initialization is fast and successful. The connection freezes at the authorization stage (simple_bind_s). Authorization takes about 15 minutes, but it should be up to a minute. What could be the problem and why is it taking so long to log in to active directory?
I tryed abother library ldap3
import json
from ldap3 import Server, Connection, ALL, NTLM
class LDAPapi:
def __init__(self, server: str, login: str, password :str , domain: str = "passport"):
self.server = Server('server', get_info=ALL)
self.conn = Connection(server, user=f'{domain}\\{login}', password=password, authentication=NTLM)
And got the same problem, it gets 10-15 minutes to connect on line Connection(server, user=f'{domain}\{login}', password=password, authentication=NTLM)
trace_level=2
((17, 3), {})
=> result:
None
*** <ldap.ldapobject.SimpleLDAPObject object at 0x7> ldap://<host>- SimpleLDAPObject.set_option
((17, 3), {})
=> result:
None
*** <ldap.ldapobject.SimpleLDAPObject object at 0x7> ldap://<host>- SimpleLDAPObject.set_option
((8, 0), {})
=> result:
None
*** <ldap.ldapobject.SimpleLDAPObject object at 0x7> ldap://<host>- SimpleLDAPObject.simple_bind
((<login>, <password>, None, None), {})
=> result:
1
*** <ldap.ldapobject.SimpleLDAPObject object at 0x7> ldap://<host>- SimpleLDAPObject.result4
((1, 1, -1, 0, 0, 0), {})
=> result:
(97, [], 1, [])
<ldap.ldapobject.SimpleLDAPObject object at 0x7>
*** <ldap.ldapobject.SimpleLDAPObject object at 0x7> ldap://<host>- SimpleLDAPObject.unbind_ext
((None, None), {})
=> result:
None```