Проблема преобразования LDAPS-соединения из ldap3 в django_auth_ldap в Python Django

Проблема получения аутентификации пользователей LDAPS в Django

Работает с модулем ldap3

Когда я запускаю приведенный ниже код, ответ показывает, что сервер LDAP смог подключиться и найти пользователя с общим именем "MyUser" в базе поиска "ou=grouping,dc=example,dc=ad"

import ldap3

#Set up LDAP connection and bind
server = ldap3.Server('ldaps://ad.example.com', use_ssl=True)
conn = ldap3.Connection(server, 'cn=crunchy-test,cn=users,dc=example,dc=ad', 'PASSWORD')
conn.bind()

#Check if bind was successful
if conn.bound:
    print('Successfully bound to LDAP server')
else:
    print('Failed to bind to LDAP server')

#Specify search base and search filter
search_base = 'ou=grouping,dc=example,dc=ad'
search_filter = "(cn=MyUser)" 

#Perform search and specify search scope as subtree
conn.search(search_base, search_filter, search_scope=ldap3.SUBTREE, attributes=['givenName', 'sn', 'mail'])

#Iterate over search results
for entry in conn.response:
    print(entry['dn'], entry['attributes']['givenName'], entry['attributes']['sn'], entry['attributes']['mail'])

#Unbind from LDAP server
conn.unbind()
Выход
Successfully bound to LDAP server
CN=MyUser,OU=userGroup,OU=Grouping,DC=example,DC=ad My User My.User@email.com

Работает не с django_auth_ldap , модуль ldap

#Configuration for LDAP login authentication - https://django-auth-ldap.readthedocs.io/en/latest/authentication.html

#Baseline configuration.
AUTH_LDAP_SERVER_URI = "ldaps://ad.example.com"

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

#--------test search bind------
AUTH_LDAP_BIND_DN = "cn=crunchy-test,cn=users,dc=example,dc=ad"
AUTH_LDAP_BIND_PASSWORD = "PASSWORD"
AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=grouping,dc=example,dc=ad",ldap.SCOPE_SUBTREE,"(cn=%(user)s)")

#---ldap user login config
#A dictionary of options to pass to each connection to the LDAP server
AUTH_LDAP_CONNECTION_OPTIONS = {
    ldap.OPT_REFERRALS: 0,
    ldap.OPT_PROTOCOL_VERSION: 3,
    }

#A string template that describes any user’s distinguished name based on the username. 
AUTH_LDAP_USER_DN_TEMPLATE = "cn=%(user)s,ou=grouping,dc=example,dc=ad" 
AUTH_LDAP_BIND_AS_AUTHENTICATING_USER = True

#---ldap group DN config
#All groups referenced elsewhere in the configuration must be of this type(AUTH_LDAP_GROUP_TYPE) and part of the search results(AUTH_LDAP_GROUP_SEARCH)
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("cn=crunchy-test,ou=example,ou=ldaptree,ou=specificgroups,dc=example,dc=ad", ldap.SCOPE_SUBTREE)
AUTH_LDAP_GROUP_TYPE = GroupOfNamesType(name_attr='cn')

#Populate Django user from the LDAP directory.
AUTH_LDAP_USER_ATTR_MAP = {
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail"
}
Сообщение об ошибке
Authentication failed for MyUser: user DN/password rejected by LDAP server.\r, referer: mysite-uat.example.com/site/login

Выводы

Не могу заставить его работать с кодом, который у меня есть в settings.py Я могу подтвердить, что пароль правильный, потому что соединение и поиск работают с ldap3. Все аргументы, такие как база поиска, пароль, URI и т.д. были изменены для конфиденциальности.

Вернуться на верх