Skip to content

Instantly share code, notes, and snippets.

@kelverarruda
Created April 28, 2024 16:13
Show Gist options
  • Save kelverarruda/9b0f7c3b4f4e82df39a0f29b543cd9a4 to your computer and use it in GitHub Desktop.
Save kelverarruda/9b0f7c3b4f4e82df39a0f29b543cd9a4 to your computer and use it in GitHub Desktop.
How to test login in AD using python
from ldap3 import Server, Connection, SIMPLE, SYNC, ALL
def authenticate_user(username, password, domain_controller):
server = Server(domain_controller, get_info=ALL)
try:
conn = Connection(server, user=username, password=password, authentication=SIMPLE, auto_bind=True)
if conn.bound:
print(f"User '{username}' authenticated successfully.")
conn.unbind()
return True
else:
print(f"Failed to authenticate user '{username}'.")
return False
except Exception as e:
print(f"An error occurred: {str(e)}")
return False
if __name__ == "__main__":
username = "<username>"
password = "<password>"
domain_controller = "ldap://<domain>"
authenticate_user(username, password, domain_controller)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment