Python ldap實現登入

來源:互聯網
上載者:User
下面一段代碼是小編給大家介紹的Python ldap實現登入執行個體代碼,一起看看吧

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': '111111.0',  'original_pass': '111111.0'}ldap_message = {  0: 0, #'ok'  1: 1, #'使用者名稱或密碼錯誤'  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']  _ldap_user = ldap_config['ldap_user']  _ldap_pass = ldap_config['ldap_pass']  _original_pass = ldap_config['original_pass']  # 串連ldap伺服器  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  # 驗證使用者登入  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      # 將上一步計算的id在下面運算      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': ''}    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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.