【问题标题】:How can I verify user and password using Python ldap3 via OpenLdap?如何通过 OpenLdap 使用 Python ldap3 验证用户和密码?
【发布时间】:2020-12-27 17:05:37
【问题描述】:

对于一个 django 项目,我设计了一个不同的登录页面。这里的用户将通过openldap登录。

我可以使用用户的 uid id 访问用户的完整信息,但我找不到如何验证密码。

我是否需要对用户密码进行哈希处理并将其与 ldap 上的密码进行比较?难道没有其他方法了吗?谢谢

from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3.core.exceptions import LDAPException, LDAPBindError, LDAPSocketOpenError
from ldap3.utils.conv import escape_filter_chars

ldap_server_uri=f"ldap://xxx:389"
ldap_base = 'dc=xx,dc=xx,dc=xx'

def ldap(uid,password):
    try:     
        ldap_server = Server(ldap_server_uri, get_info=ALL)
        ldap_connection = Connection(ldap_server, user = 'uid=admin,ou=xx,dc=xx,dc=xx',password='adminpassword')
        if ldap_connection.bind() == True:
            if ldap_connection.search(search_base=ldap_base, search_filter=f'(uid={uid})',search_scope = SUBTREE, attributes=['uid']) == True:
                ent = ldap_connection.entries[0]
                entry = {'uid': ent['uid']}
                ldap_connection.unbind()
                return entry
            else:
                return None
    except LDAPSocketOpenError:
        print('Unabled to connect to the LDAP server!')
        return None

【问题讨论】:

    标签: python django openldap


    【解决方案1】:

    只是为了检查我使用的用户名和密码:

    import ldap3
    from ldap3.core.exceptions import LDAPException
    
    
    def _ldap_login(username, password):
        try:
            with ldap3.Connection('enter_server', user=username, password=password) as conn:
                print(conn.result["description"]) # "success" if bind is ok
                return True
        except LDAPException:
            print('Unable to connect to LDAP server')
            return False
    
    _ldap_login("enter_username", "enter_password")
    

    以下是30 code examples,用于展示如何使用ldap3Tutorial: Introduction to ldap3

    【讨论】:

    • 总而言之,在您看来,检查给定用户是否可以直接登录到 ldap 就足够了,对吗?那么ldap中就不能列出不能登录的用户吗?
    • 没错,你没看错。 DBA 或其他管理 LDAP 的人将向您详细解释这一点。在我的示例中,在 LDAP 中只有具有该公司域的用户,甚至其他人甚至无法访问登录页面。现在,您可能会遇到这种检查还不够的情况,即应用程序本身具有多个角色。例如,它必须来自公司的域,而且它必须在可以管理应用程序的组中,否则它只能读取数据。就像使用标准数据库一样 - 这完全取决于应用程序的用途。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多