<tfoot id='EqUPL'></tfoot>

    1. <i id='EqUPL'><tr id='EqUPL'><dt id='EqUPL'><q id='EqUPL'><span id='EqUPL'><b id='EqUPL'><form id='EqUPL'><ins id='EqUPL'></ins><ul id='EqUPL'></ul><sub id='EqUPL'></sub></form><legend id='EqUPL'></legend><bdo id='EqUPL'><pre id='EqUPL'><center id='EqUPL'></center></pre></bdo></b><th id='EqUPL'></th></span></q></dt></tr></i><div id='EqUPL'><tfoot id='EqUPL'></tfoot><dl id='EqUPL'><fieldset id='EqUPL'></fieldset></dl></div>
      • <bdo id='EqUPL'></bdo><ul id='EqUPL'></ul>

      <small id='EqUPL'></small><noframes id='EqUPL'>

      <legend id='EqUPL'><style id='EqUPL'><dir id='EqUPL'><q id='EqUPL'></q></dir></style></legend>

      了解 Django-LDAP 身份验证

      Understanding Django-LDAP authentication(了解 Django-LDAP 身份验证)
    2. <legend id='3Urvd'><style id='3Urvd'><dir id='3Urvd'><q id='3Urvd'></q></dir></style></legend>

          <tbody id='3Urvd'></tbody>

      1. <i id='3Urvd'><tr id='3Urvd'><dt id='3Urvd'><q id='3Urvd'><span id='3Urvd'><b id='3Urvd'><form id='3Urvd'><ins id='3Urvd'></ins><ul id='3Urvd'></ul><sub id='3Urvd'></sub></form><legend id='3Urvd'></legend><bdo id='3Urvd'><pre id='3Urvd'><center id='3Urvd'></center></pre></bdo></b><th id='3Urvd'></th></span></q></dt></tr></i><div id='3Urvd'><tfoot id='3Urvd'></tfoot><dl id='3Urvd'><fieldset id='3Urvd'></fieldset></dl></div>
      2. <tfoot id='3Urvd'></tfoot>

          <small id='3Urvd'></small><noframes id='3Urvd'>

              <bdo id='3Urvd'></bdo><ul id='3Urvd'></ul>
              1. 本文介绍了了解 Django-LDAP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Django 新手,我被分配了以 LDAP 作为后端实现用户身份验证系统的任务.我猜 documentation 假设最终开发人员在 Django 方面有足够的经验能够了解和实施这样的系统.这是我无法理解如何使用基于 LDAP 的身份验证实现简单 django 应用程序的地方.以下是我目前所理解的:

                I am new to Django and have been assigned the task of implementing a user authentication system with LDAP as the backend. I guess the documentation assumes that the end developer has enough experience in Django to be able to understand and implement such a system. This is where I fail to understand how to implement a simple django application with LDAP based authentication. Here is what I have understood so far:

                仅将更改发布到文件:

                settings.py
                ....
                import ldap
                from django_auth_ldap.config import LDAPSearch
                
                AUTH_LDAP_SERVER_URI = "ldap://<my url>" 
                AUTHENTICATION_BACKENDS = ('django_auth_ldap.backend.LDAPBackend')
                
                AUTH_LDAP_CONNECTION_OPTIONS = { 
                    ldap.OPT_REFERRALS: 0
                }
                
                MIDDLEWARE_CLASSES = ( 
                     ....
                    'django.contrib.sessions.middleware.SessionMiddleware',
                    'django.contrib.auth.middleware.AuthenticationMiddleware',
                    ...
                )
                
                INSTALLED_APPS = ( 
                    'django.contrib.auth',
                    'django.contrib.contenttypes',
                    'django.contrib.sessions',
                    ....
                )
                

                auth.html

                <html>
                    <head>
                        <title>Login</title>
                    </head>
                    <body>
                        {{state}}
                        <form action="" method="post"> {% csrf_token %}
                            Email address: <input type="text" name="email" value="{{ email }}" />
                            Password: <input type="password" name="password" value="" />
                            <input type="submit" value="Log in" />
                        </form>
                    </body>
                </html>
                

                models.py:

                ??
                

                views.py:

                from django.shortcuts import render_to_response
                from django.contrib.auth import authenticate, login
                from django.template import RequestContext
                
                
                def login_user(request):
                
                    username = password = ""
                    state = ""
                
                    if request.POST:
                        username = request.POST.get('username')
                        password = request.POST.get('password')
                
                        print username, password
                
                        user = authenticate(username=username, password=password)
                        if user is not None:
                            login(request, user)
                            state = "Valid account"
                        else:
                            state = "Inactive account"
                    return render_to_response('auth_user/auth.html', RequestContext(request, {'state': state, 'username': username}))
                

                我无法理解什么?

                1> 我很确定我必须在 views.py 中实现一个函数来获取 emailPOST 值代码>密码并验证它,例如:[SO].该文档指定实现搜索/绑定或直接绑定.为什么?如果 views.py 将包含实际的身份验证代码,那么文档中指定的代码是做什么的?

                1> I am pretty sure I would have to implement a function in views.py to get the POST values for email and password and validate it, e.g: [SO]. The documentation specifies to either implement a Search/Bind or Direct Bind. Why? If the views.py would contain the actual piece of authentication code, what is the code doing specified in the documentation?

                2> 如果 views.py 会执行实际的身份验证,那么为什么我们需要文档中指定的变量?

                2> If the views.py would perform the actual auth, then why do we need the variable specified in the documentation?

                3> 作者在这个库方面做得很好,但文档没有提供一个简单的准系统示例来说明如何使用 LDAP 实现整个身份验证系统.如果存在,任何人都可以指出这样的资源吗?要实现这样的系统,需要添加/修改的文件并不容易理解.

                3> The author has done a great job with the library, but the documentation does not provide with a simple barebones example of how to implement the entire authentication system with LDAP. Can anyone please point to such a resource, if it exists? It is not easy to understand the files that need to be added/modified to implement such a system.

                推荐答案

                这个页面可能有你要找的东西:https://pypi.python.org/pypi/django-auth-ldap 关于 LDAP 后端.你很幸运有一个存在,所以你不必自己编写身份验证后端:-)

                This page might have what you are looking for: https://pypi.python.org/pypi/django-auth-ldap concerning the LDAP backend. You are lucky that one exists, so you don't have to code an auth backend yourself :-)

                基本上 django.contrib.auth.models 已经有一个 User 对象,其中包含您需要的有关用户的所有信息.所以你不需要创建一个新的models.py.

                Basically django.contrib.auth.models already has a User object that contains everything you need about the user. So you don't need to create a new models.py.

                你只需要在你的views.py中验证你自己的身份,在一个登录功能中,使用

                You just need to authenticate yourself in your views.py, in a login function, using

                from django.contrib.auth import authenticate, login
                user = authenticate(username=request.REQUEST.get('email'), password=request.REQUEST.get('password'))
                # handle error cases, inactive users, ...
                login(request, user)
                

                如果用户为无,则身份验证失败.如果没有,您可以探索这个对象,看看后端为您提取了什么.

                If user is None, then authentication failed. If not, you can explore this object to see what has the backend pulled for you.

                然后,如果您希望将此应用程序的 Preferences 链接到此用户,而不是 LDAP 的一部分,您可以选择创建另一个模型,并将 User 作为 foreignKey.

                Then, you can elect to create another model with User as a foreignKey if you want to keep Preferences linked to this User for this application but nor part of the LDAP.

                在这种情况下,您将需要:

                In this case, you will need:

                Models.py

                根据您的应用定义对您很重要的数据.您将从 LDAP 中提取用户数据,并用它和其他与用户关联的首选项填充此模型:

                The definition of the data that is important to you based on your application. You are going to pull the user data from the LDAP, and fill up this model with it and other preferences linked to the User:

                from django.contrib.auth.models import User    
                
                class Profile(models.Model):
                    """User profile.  Contains some basic configurable settings"""
                    user = models.ForeignKey(User, unique=True)
                    phone_number = models.CharField(max_length=256, blank=True, default='')
                    ...
                

                Views.py

                • 在登录函数中,如果 request.method == 'POST',则 get_or_create 使用您刚刚从身份验证获得的用户创建用户配置文件.

                • in the login function, if request.method == 'POST', then get_or_create the user profile using the user your just got from authenticate.

                profile, profile_is_new = Profile.objects.get_or_create(user=user)
                

                这篇关于了解 Django-LDAP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)

                  <tbody id='p0MiQ'></tbody>
                  <bdo id='p0MiQ'></bdo><ul id='p0MiQ'></ul>

                      <legend id='p0MiQ'><style id='p0MiQ'><dir id='p0MiQ'><q id='p0MiQ'></q></dir></style></legend>
                      <i id='p0MiQ'><tr id='p0MiQ'><dt id='p0MiQ'><q id='p0MiQ'><span id='p0MiQ'><b id='p0MiQ'><form id='p0MiQ'><ins id='p0MiQ'></ins><ul id='p0MiQ'></ul><sub id='p0MiQ'></sub></form><legend id='p0MiQ'></legend><bdo id='p0MiQ'><pre id='p0MiQ'><center id='p0MiQ'></center></pre></bdo></b><th id='p0MiQ'></th></span></q></dt></tr></i><div id='p0MiQ'><tfoot id='p0MiQ'></tfoot><dl id='p0MiQ'><fieldset id='p0MiQ'></fieldset></dl></div>
                      <tfoot id='p0MiQ'></tfoot>
                        • <small id='p0MiQ'></small><noframes id='p0MiQ'>