我正在尝试使用LDAPBackend在Django中对用户进行身份验证,但遇到错误

时间:2019-08-28 08:09:44

标签: active-directory python-3.6 django-1.11 django-auth-ldap

我想从我的django应用程序对Active Directory用户进行身份验证。我正在使用Python 3.6.8,Django 1.11.18和django-auth-ldap == 2.0.0的版本

我一直关注该文章,直到“此时您应该可以尝试使用Django Auth LDAP后端登录!”这一行:https://coderbook.com/@marcus/how-to-add-ldap-and-active-directory-authentication-to-django/

我的settings.py文件:

"""
Django settings for ADTest project.

Generated by 'django-admin startproject' using Django 1.11.18.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os


import ldap
from django_auth_ldap.config import LDAPSearch


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 't3(r5i0(jq2lmx#yw1zsu21a&*=)k&6+pwj%ht=o_^_$=s^ttk'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

AUTH_LDAP_SERVER_URI = os.environ.get("ldap://192.168.5.50:1025")
AUTH_LDAP_ALWAYS_UPDATE_USER = True
AUTH_LDAP_BIND_DN = os.environ.get("administrator")
AUTH_LDAP_BIND_PASSWORD = os.environ.get("xsdsdfd")
AUTH_LDAP_USER_SEARCH = LDAPSearch("", ldap.SCOPE_SUBTREE, "sAMAccountName=%(username)s")
# AUTH_LDAP_USER_SEARCH = LDAPSearch(
#     '',
#     ldap.SCOPE_SUBTREE,
#     '(uid=%(user)s)',
# )
AUTH_LDAP_USER_ATTR_MAP = {
    "username": "sAMAccountName",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
}

AUTHENTICATION_BACKENDS = [
    #"django.contrib.auth.backends.ModelBackend",
    "django_auth_ldap.backend.LDAPBackend",
]

ROOT_URLCONF = 'ADTest.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'ADTest.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

我的view.py文件:

import sys

from django.contrib.auth import authenticate
from django.views.decorators.csrf import csrf_exempt
from django.http.response import JsonResponse

@csrf_exempt
def UserLogin(request):

    try:
        username = 'ADUser'
        password = 'User1234!11'
        xyz = authenticate(username=username, password=password)
        print(xyz)
    except:
        realMessage = str(sys.exc_info()[1])
        print(realMessage)

    return JsonResponse({"Hello":"User"})

致电UserLogin服务时遇到的错误: “ initialize()参数1必须为str,而不是None

enter image description here

请让我知道我做错了什么?我是Ldap和Active Directory的新手。预先感谢。

0 个答案:

没有答案