Microsoft Ldap login using python3
Install dependent packages
python3 -m pip install ldap3
Sample Code to test login
from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3.core.exceptions import LDAPException, LDAPBindError
def connect_ldap_server(SERVER_URI, DN,USERNAME, PASSWORD):
try:
# Provide the hostname and port number of the openLDAP
server = Server(SERVER_URI, get_info=ALL)
# username and password can be configured during openldap setup
connection = Connection(server,
user='CN='+USERNAME+','+DN,
password=PASSWORD)
bind_response = connection.bind() # Returns True or False
return bind_response
except LDAPBindError as e:
connection = e
return False
# print(connection)
# print(bind_response)
if connect_ldap_server('ldap://9.1.0.3','OU=Headoffice,DC=example,DC=com', 'testuser', 'testpassword'):
print('User logged in successfully')
else:
print('User log in was unsuccessful')
Comments
Post a Comment