This article discusses using PYTHON-LDAP to implement the login method
Ldap_config = {' Ldap_path ': ' ldap://xx.xx.xx.xx:389 ', ' base_dn ': ' ou=users,dc=ledo,dc=com ', ' ldap_user ': ' Uid=re Porttest,ou=users,dc=ledo,dc=com ', ' ldap_pass ': ' 111111.0 ', ' original_pass ': ' 111111.0 '}ldap_message = {0:0, # ' Ok ' 1:1, # ' User name or password error ' 2:2, #ldap验证异常 '}import ldapimport base64import hashlibfrom config_message import Ldap_config, Ldap_messageclass Ldap_api (object): _ldap_path = ldap_config[' Ldap_path '] _base_dn = ldap_config[' base_dn '] _lda P_user = ldap_config[' Ldap_user '] _ldap_pass = ldap_config[' Ldap_pass '] _original_pass = ldap_config[' Original_pass ' ] # Connect 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) except LDAP. Ldaperror, e:print e # Verify user login def ldap_check_login (self, username, password): obj = Self.ldapconnSearchScope = LDAP. Scope_subtree # searchfilter = ' (& (cn= ' +username+ ') (userpassword= ' +password+ ')) ' Searchfilter = ' uid= ' + Username Try:obj.search (self._base_dn, SearchScope, Searchfilter, None) # id--2 # i is calculated in the previous step D in the following operation 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 (PA ssWOrd) if L_password in (password, Md_password): return {' status ': ldap_message[0], ' data ': L_r Ealname} else:return {' status ': ldap_message[1], ' Data ': '} except 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