【问题标题】:Attempting to connect to LDAP using django-python3-ldap but the target machine is actively refusing it尝试使用 django-python3-ldap 连接到 LDAP,但目标机器主动拒绝它
【发布时间】:2018-05-25 18:16:15
【问题描述】:

我正在开发一个 django 应用程序并尝试使用 django_python3_ldap 连接到我公司的 LDAP 和 AD。我按照settings.py 中的文档基本设置进行操作,但是当我尝试执行python manage.py ldap_sync_users 时,出现此错误

LDAP connect failed: ('unable to open socket', [(datetime.datetime(2018, 5, 25, 12, 25, 36, 77877), <class 'ldap3.core.exceptions.LDAPSocketOpenError'>, LDAPSocketOpenError('socket connection error while opening: [WinError 10061] No connection could be made bec
ause the target machine actively refused it',), ('::1', 389, 0, 0)), (datetime.datetime(2018, 5, 25, 12, 25, 37, 78809), <class 'ldap3.core.exceptions.LDAPSocketOpenError'>, LDAPSocketOpenError('socket connection error while opening: [WinError 10061] No connect
ion could be made because the target machine actively refused it',), ('127.0.0.1', 389))])
CommandError: Could not connect to LDAP server

这是我的 settings.py 文件

import os
import ldap
from django_auth_ldap.config import LDAPSearch, GroupOfNamesType, NestedActiveDirectoryGroupType

# Baseline configuration.
AUTH_LDAP_SERVER_URI = "ldap://***.**.***.**"
LDAP_AUTH_CONNECTION_USERNAME = 'usr@ab.com'
LDAP_AUTH_CONNECTION_PASSWORD = '*******'

# The LDAP search base for looking up users.
LDAP_AUTH_SEARCH_BASE = "ou=people,dc=example,dc=com"

# The LDAP class that represents a user.
LDAP_AUTH_OBJECT_CLASS = "user"

# Keep ModelBackend around for per-user permissions and maybe a local
# superuser.
AUTHENTICATION_BACKENDS = (
    'django_python3_ldap.auth.LDAPBackend',
    'django.contrib.auth.backends.ModelBackend',
)
DEBUG = True

LDAP_AUTH_USE_TLS = False


# User model fields mapped to the LDAP
# attributes that represent them.
LDAP_AUTH_USER_FIELDS = {
    "username": "sAMAccountName",
    "first_name": "givenName",
    "last_name": "sn",
    "email": "mail",
}


LDAP_AUTH_FORMAT_USERNAME = "django_python3_ldap.utils.format_username_active_directory_principal"
LDAP_AUTH_ACTIVE_DIRECTORY_DOMAIN = "ab.com"


LDAP_AUTH_USER_LOOKUP_FIELDS = ("username",)

LDAP_AUTH_CLEAN_USER_DATA = "django_python3_ldap.utils.clean_user_data"

LDAP_AUTH_SYNC_USER_RELATIONS = "django_python3_ldap.utils.sync_user_relations"

LDAP_AUTH_FORMAT_SEARCH_FILTERS = "django_python3_ldap.utils.format_search_filters"

但我认为这不是配置问题,因为从我看到的所有 google 帖子和 SO 帖子中,这似乎是公司防火墙阻止了我的访问?或者我使用的LDAP_AUTH_CONNECTION_USERNAME 没有权限?有没有其他方法可以发生这种情况?

【问题讨论】:

    标签: python django active-directory ldap


    【解决方案1】:

    该错误表示服务器(域控制器或中间设备)正在停止连接。可能是没有到域控制器的网络连接。

    在 Windows 上,您可以在 PowerShell 中使用您的域名代替“domain.com”来测试网络连接

    (New-Object Net.Sockets.TcpClient).Connect("domain.com", 389)
    

    无输出表示成功。如果失败,它会告诉你一个大的红色错误消息。

    如果这不起作用,请尝试其中一种。 AD LDAP 可以在 4 个端口中的任何一个上工作:

    • 389:LDAP - 读取/写入单个域 - 如果您不指定端口,这是默认设置
    • 636:LDAP over SSL - 与 389 相同,但已加密
    • 3268:全局目录 - 对您的 AD 林只读(如果您有多个域,否则它与 389 没有什么不同,只读除外)
    • 3269:基于 SSL 的全局目录

    如果其他端口之一有效,您可以在代码中指定它,通常如下所示:“LDAP://domain.com:3268”

    我知道AD,但我对python不太了解,所以我无法帮助你具体说明。

    【讨论】:

    • 谢谢!我也是这么想的,但我不太明白为什么我可以通过单点登录身份验证建立连接,所以如果我用 conn = ldap.initialize('ldap://192.168.122.64' 登录) # Windows 服务器的 IP 地址 >>> conn.protocol_version = 3 >>> conn.set_option(ldap.OPT_REFERRALS, 0) >>> conn.simple_bind_s('Administrator@mooctest.local', 'yourpassword') #一个要测试的帐户(97,[],2,[])它可以工作,但是当我将它放在我的应用程序上时会出现此错误?关于为什么的任何想法?
    • 我现在才注意到这一点,但其中有一个例外:('127.0.0.1', 389)。好像没有用你刚才给的那个192 IP。
    • 那个 192 IP 地址只是例如,这不是我的实际 IP,但这是否意味着我提供的任何 IP 都没有使用该 IP 地址?所以我一直提供错误的 IP 地址?
    • 我不能说你是否给它错误的IP,但它似乎正在尝试连接到本地机器。唯一可行的方法是在域控制器上运行它。
    • 您是否尝试过使用域名 (LDAP://domain.com) 而不是 IP?
    猜你喜欢
    • 1970-01-01
    • 2021-10-08
    • 2011-04-04
    • 1970-01-01
    • 2011-10-10
    • 2013-07-15
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    相关资源
    最近更新 更多