Use python-ldap for logon

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

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.