I made the ldap connection successfully. How to get:
1. I have Python v 3.7.2
2. Install python-ldap: for this I tried pip install python-ldap , but it didn’t work for me on a Windows machine, so I use the alternative below.
3.To install ldap, follow the link: https://www.lfd.uci.edu/~gohlke/pythonlibs/#python-ldap and download python_ldap - 3.1.0 - cp37 - cp37m - win_amd64.whl
4. Now go to the download directory and run " pip install python_ldap - 3.1.0 - cp37 - cp37m - win_amd64.whl "
- Now open the Python shell and check "import ldap", if you do not get any errors, then everything is ready.
This is a sample code:
#Resource of code :https://gist.github.com/ibeex/1288159 import ldap def check_credentials(username, password): """Verifies credentials for username and password. Returns None on success or a string describing the error on failure # Adapt to your needs """ LDAP_SERVER = 'xxx' # fully qualified AD user name LDAP_USERNAME = '% s@spi.com ' % username # your password LDAP_PASSWORD = password base_dn = 'DC=spi,DC=com' ldap_filter = 'userPrincipalName=% s@spi.com ' % username attrs = ['memberOf'] try: # build a client ldap_client = ldap.initialize(LDAP_SERVER) # perform a synchronous bind ldap_client.set_option(ldap.OPT_REFERRALS,0) ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD) except ldap.INVALID_CREDENTIALS: #print("wron") ldap_client.unbind() return 'Wrong username or password' except ldap.SERVER_DOWN: #print("down") return 'AD server not awailable' # all is well # get all user groups and store it in cerrypy session for future use ab = str(ldap_client.search_s(base_dn, ldap.SCOPE_SUBTREE, ldap_filter, attrs)[0][1]['memberOf']) #print("ab"+ab) ldap_client.unbind() return 'success' if __name__ == "__main__": u="chirag" p="secred" print(check_credentials(u,p))
chirag
source share