如何将Django ldap用户添加到Django模型后端组

2024-06-17 13:41:22 发布

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

我使用ModelBackend(默认)运行Django-1.7.7,只有很少的用户和2个组。在

现在我已经用模型后端实现了Ldap后端。 但是在这之后,当用户进行身份验证时,我必须自动将所有经过Ldap身份验证的用户添加到其中一个模型组中。在

有没有办法做到这一点?在


Tags: django用户模型身份验证ldap办法modelbackend
2条回答

您可以避免使用Django LDAP特性在模型中存储用户。你应该向你的LDAP服务器“询问”该用户的凭据是否正确,然后你就可以做你的事情:将用户名存储在一个可变会话中,重定向到特定的页面,在每个页面中,你可以检查带有用户名的变量会话是否正确,等等,。。。在

在设置.PY在

# LDAP Configuration.
import ldap
from django_auth_ldap.config import LDAPSearch

AUTHENTICATION_BACKENDS = (
    'django_auth_ldap.backend.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)

# Binding and connection options.

# Address (by IP or Hostname) of LDAP Server (Directory Active)
AUTH_LDAP_SERVER_URI = "ldap://xxx.xxx.x.xxx"
# DN of user through we bind to LDAP Server.
AUTH_LDAP_BIND_DN = "CN=xxx, CN=xxx, DC=xxx, DC=xxx"
# Password of user through we bind to LDAP Server.
AUTH_LDAP_BIND_PASSWORD = "xxxxxx"
# Node where we start to search users. Use to be DN (of random user) without the last one parameter.
AUTH_LDAP_USER_SEARCH = LDAPSearch("CN=xxx, DC=xxx, DC=xxx", ldap.SCOPE_SUBTREE, "(samAccountName=%(user)s)")

然后可以在视图中使用它,以检查是否存在特定用户:

在PY.PY视图在

^{pr2}$

相关问题 更多 >