无法使用Django和ldap进行身份验证

2024-09-27 21:29:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是一个简单的表单,它接受用户名和密码,并尝试使用ldap服务器对该用户进行身份验证。仪表板的实际门户是用php编写的,但我必须使用django(和pyldap)重写它。在我的设置.py文件,我有以下设置(我修改了URI,因为它是由我所在的公司使用的)。你知道吗

# LDAP Settings
# Set backends
AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
)

# LDAP Server
AUTH_LDAP_SERVER_URI = 'ldaps://eddie.xy.zzzzz.com:3268'
AUTH_LDAP_START_TLS = True

# Search settings
AUTH_LDAP_BIND_DN = ""
AUTH_LDAP_BIND_PASSWORD = ""
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=xy,dc=zzzzz,dc=com", ldap.SCOPE_SUBTREE, "(uid=%(user)s)")

在我的视图.py,我有以下代码(只是为了测试东西)

from django.shortcuts import render
from django.contrib.auth import authenticate
from django_auth_ldap.backend import LDAPBackend # Greyed out

def login(request):
    if request.method == 'POST':
        # Get info from POST request
        usr = request.POST['username']
        pas = request.POST['password']

        # Authenticate the user using provided data
        user = authenticate(username=usr, password=pas)
        print(str(user))

        if user is not None:
            return render(request, 'dashboard/index.html', {})
        else:
            print("The username and password were incorrect.")
            return render(request, 'dashboard/error.html', {})

    elif request.method == 'GET':
        return render(request, 'dashboard/login.html', {})

你知道吗登录.html包含一个简单的HTML代码,其中包含一个表单,method=“post”action=“/login/”。索引.html包含简单的“成功”消息,以及错误.html包含一个简单的“错误!”信息。我正在使用我的(工作)凭证的形式,我做,但我不能得到一个成功的消息。你知道吗

我也试过这样做:

at = LDAPBackend()
user = authenticate(username=usr, password=pas)

而不是上面代码中使用的authenticate方法。不幸的是,我也有同样的行为。我错过了什么,错在哪里?你知道吗


Tags: djangofromauthrequesthtmlusernamepasswordrender
1条回答
网友
1楼 · 发布于 2024-09-27 21:29:15

最后,我设法做到了。我必须设置这个变量:

AUTH_LDAP_USER_DN_TEMPLATE = "%(user)s"

现在它工作得很好。我希望这能帮助其他人。无论如何谢谢你!你知道吗

祝你今天愉快!你知道吗

相关问题 更多 >

    热门问题