【问题标题】:Django-Ldap-AuthenticationDjango-Ldap-身份验证
【发布时间】:2017-06-03 23:42:34
【问题描述】:

我正在尝试使用 django 中的 LDAP 服务器对用户进行身份验证。

我的 settings.py 配置如下:

AUTH_LDAP_SERVER_URI = "ldap.forumsys.com"
AUTH_LDAP_BIND_DN = "cn=read-only-admin,dc=example,dc=com"
AUTH_LDAP_BIND_PASSWORD = "password"
AUTH_LDAP_USER_SEARCH = LDAPSearch("dc=example,dc=com",
    ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
AUTH_LDAP_START_TLS = True


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

在我看来,我尝试使用 LDAPBACKEND 对其进行身份验证

from django.http import HttpResponse
from django_auth_ldap.backend import LDAPBackend
from django.contrib.auth.models import User


from django.conf import settings


def login_user(request):

    state = ""

    username = settings.AUTH_LDAP_BIND_DN
    password = settings.AUTH_LDAP_BIND_PASSWORD

    auth = LDAPBackend()

    try:
        User = auth.authenticate(username=username,password=password) 
        if User is not None:
            state = "Valid"

        else:
            state = "Invalid"

    except LDAPError as e:
            state = "Error"

    return HttpResponse(state)  

但我收到一个错误

验证 cn=read-only-admin,dc=example,dc=com 时出现 LDAPError: LDAPError(0,'错误')

我还有另一个疑问。 usernamepassword 是否与 bind_usernamebind_password 相同?

【问题讨论】:

    标签: python django python-2.7 authentication ldap


    【解决方案1】:

    确保 AUTH_LDAP_SERVER_URI 应该是 AD 的主机名或 IP 地址。 在 django settings.py 中:

    AUTH_LDAP_SERVER_URI = "ldap://hostname or Ip address of active directory"
    AUTH_LDAP_BIND_DN = "CN=sAMAccountName,CN=Users,DC=yourdomain,DC=com"
    AUTH_LDAP_BIND_PASSWORD = *******
    AUTH_LDAP_CONNECTION_OPTIONS = {
        ldap.OPT_REFERRALS: 0,
    }
    AUTH_LDAP_USER_SEARCH = LDAPSearch('CN=Users,DC=yourdomain,DC=com', 
    ldap.SCOPE_SUBTREE, "userPrincipalName=%(user)s")
    
    AUTHENTICATION_BACKENDS = (
        'django_auth_ldap.backend.LDAPBackend',
        'django.contrib.auth.backends.ModelBackend')
    

    而views.py应该是这样的,

    from django.contrib.auth import views as auth_views
    from forms import ProjectRequestForm, ExAuthenticationForm
    
    def login(request):
        return auth_views.login(request, template_name='login.html', authentication_form=ExAuthenticationForm)
    

    【讨论】:

      【解决方案2】:

      我在 LDAP 方面的经验并不要求任何视图更改。我使用了 django-auth-ldap 库,它只需要额外的设置即可使用:

      #-----------------------------------------------------------------------------#
      #
      #   LDAP Settings
      #
      #-----------------------------------------------------------------------------#
      
      AUTHENTICATION_BACKENDS += ('django_auth_ldap.backend.LDAPBackend',) 
      
      AUTH_LDAP_SERVER_URI = "ldaps://your.ldap.server"
      
      AUTH_LDAP_USER_DN_TEMPLATE = "uid=%(user)s,ou=users,dc=example,dc=com"
      

      使用绑定登录也适用于这些附加设置:

      import ldap
      from django_auth_ldap.config import LDAPSearch
      
      AUTH_LDAP_BIND_DN = "<user>"
      AUTH_LDAP_BIND_PASSWORD = "<password>"
      AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=users,dc=example,dc=com",ldap.SCOPE_SUBTREE, "(uid=%(user)s)")
      

      普通的 Django 登录视图在此设置下工作正常。

      编辑:我应该补充一点,在尝试使用 Django 之前,应该通过服务器上的命令行确认 LDAP 正在工作。这就是起初让我受不了的原因。

      【讨论】:

        【解决方案3】:

        我建议使用基于类的视图。此外,您应该将usernamepassword 分配给用户的输入。

        此外,您应该只使用authenticate() 函数。

        from django.contrib.auth import authenticate
        
        class LoginView(FormView):
            form_class = LoginForm
            success_url = reverse_lazy('main')
            template_name = 'module_name/login.html'
        
            def form_valid(self, form):
                username = form.cleaned_data['username']
                password = form.cleaned_data['password']
                user = authenticate(username=username, password=password)
        
                if user is not None and user.is_active:
                    login(self.request, user)
                        return super(LoginView, self).form_valid(form)
                else:
                    return self.form_invalid(form)
        

        【讨论】:

        • 这也失败了:/。现在我收到一个错误 SERVER_DOWN({'desc': "Can't contact LDAP server"},)
        • 那我觉得你的设置不对。您是否尝试使用 ldap 工具连接到服务器?还将端口添加到AUTH_LDAP_SERVER_URI = "ldap://ldap.forumsys.com:636" 之类的url 并检查您是否有tls 或ldaps。
        猜你喜欢
        • 2011-09-23
        • 2016-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多