python-ldap實現登入案例,python-ldap案例

來源:互聯網
上載者:User

python-ldap實現登入案例,python-ldap案例

 1 ldap_config = { 2     'ldap_path': 'ldap://xx.xx.xx.xx:389', 3     'base_dn': 'ou=users,dc=ledo,dc=com', 4     'ldap_user': 'uid=reporttest,ou=users,dc=ledo,dc=com', 5     'ldap_pass': '111111.0', 6     'original_pass': '111111.0' 7 } 8  9 ldap_message = {10     0: 0, #'ok'11     1: 1, #'使用者名稱或密碼錯誤'12     2: 2, #ldap驗證異常'13 }14 15 import ldap16 import base6417 import hashlib18 from config_message import ldap_config, ldap_message19 20 21 class LDAP_API(object):22 23     _ldap_path = ldap_config['ldap_path']24     _base_dn = ldap_config['base_dn']25     _ldap_user = ldap_config['ldap_user']26     _ldap_pass = ldap_config['ldap_pass']27     _original_pass = ldap_config['original_pass']28 29     # 串連ldap伺服器30     def __init__(self):31 32         try:33             self.ldapconn = ldap.initialize(self._ldap_path)34             self.ldapconn.protocal_version = ldap.VERSION335             self.ldapconn.simple_bind(self._ldap_user, self._ldap_pass)36 37         except ldap.LDAPError, e:38             print e39 40     # 驗證使用者登入41     def ldap_check_login(self, username, password):42 43         obj = self.ldapconn44         searchScope = ldap.SCOPE_SUBTREE45         # searchFilter = '(&(cn='+username+')(userPassword='+password+'))'46         searchFilter = 'uid=' + username47 48         try:49             obj.search(self._base_dn, searchScope, searchFilter, None)  # id--250             # 將上一步計算的id在下面運算51             result_type, result_data = obj.result(2, 0)52             if result_type != ldap.RES_SEARCH_ENTRY:53                 return {'status': ldap_message[1], 'data': ''}54             dic = result_data[0][1]55             l_realname = dic['sn'][0]56             l_password = dic['userPassword'][0]57             md_password = LDAP_API.hash_md5(password)58             if l_password in (password, md_password):59                 return {'status': ldap_message[0], 'data': l_realname}60             else:61                 return {'status': ldap_message[1], 'data': ''}62         except ldap.LDAPError, e:63             return {'status': ldap_message[2], 'data': ''}64 65     @staticmethod66     def hash_md5(data):67         md = hashlib.md5()68         md.update(str(data))69         a = md.digest()70         b = '{MD5}' + base64.b64encode(a)71         return b

連結    http://blog.csdn.net/shanliangliuxing/article/details/8266267  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.