This article describes how to use python-ldap to achieve logon. This article describes how to use python-ldap to achieve logon.
Ldap_config = {'ldap _ path': 'ldap: // xx. xx. xx. xx: 389 ', 'base _ dn': 'Ou = users, dc = ledo, dc = com', 'ldap _ user': 'uid = reporttest, ou = users, dc = ledo, dc = com ', 'ldap _ pass': '2017. 0', 'original _ pass': '2017. 0'} ldap_message = {0: 0, # 'OK' 1: 1, # 'incorrect user name or password '2: 2, # ldap verification exception '} import ldapimport base64import hashlibfrom config_message import ldap_config, ldap_messageclass LDAP_API (object): _ ldap_path = ldap_conf Ig ['ldap _ path'] _ base_dn = ldap_config ['base _ dn'] _ ldap_user = ldap_config ['ldap _ user'] _ ldap_pass = ldap_config ['ldap _ pass'] _ original_pass = ldap_config ['original _ pass'] # connect to the ldap server def _ init _ (self): try: self. ldapconn = ldap. initialize (self. _ ldap_path) self. ldapconn. protocal_version = ldap. VERSION3 self. ldapconn. simple_bind (self. _ ldap_user, self. _ ldap_pass) implements T ldap. LDAPError, e: print e # Verify user login def ldap_check_login (self, username, password): obj = self. ldapconn searchScope = ldap. SCOPE_SUBTREE # searchFilter = '(& (cn =' + username + ') (userPassword =' + password + ') 'searchfilter = 'uid =' + username try: obj. search (self. _ base_dn, searchScope, searchFilter, None) # id -- 2 # Calculate the id calculated in the previous step result_type, result_data = obj. result (2, 0) if result_type! = Ldap. RES_SEARCH_ENTRY: return {'status': ldap_message [1], 'data ': ''} dic = result_data [0] [1] l_realname = dic ['SN '] [0] l_password = dic ['userpassword'] [0] md_password = LDAP_API.hash_md5 (password) if l_password in (password, md_password): return {'status': ldap_message [0], 'data': l_realname} else: return {'status': ldap_message [1], 'data': ''} cannot ldap. LDAPError, e: return {'status': ldap_message [2], 'data': ''} @ staticmethod def hash_md5 (data): md = hashlib. md5 () md. update (str (data) a = md. digest () B = '{MD5}' + base64.b64encode (a) return B
The above describes how to use python-ldap for logon. For more information, see other related articles in the first PHP community!