Using PYTHON-LDAP to implement a login method

Source: Internet
Author: User
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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.