使用Python + LDAP身份验证对Active Directory(Authenticatin

2019-07-17 14:19发布

如何针对AD使用Python + LDAP身份验证。 我目前正在使用Python-LDAP库和所有它正在生产的泪水。

我甚至不能绑定到执行一个简单的查询:

import sys
import ldap


Server = "ldap://my-ldap-server"
DN, Secret, un = sys.argv[1:4]

Base = "dc=mydomain,dc=co,dc=uk"
Scope = ldap.SCOPE_SUBTREE
Filter = "(&(objectClass=user)(sAMAccountName="+un+"))"
Attrs = ["displayName"]

l = ldap.initialize(Server)
l.protocol_version = 3
print l.simple_bind_s(DN, Secret)

r = l.search(Base, Scope, Filter, Attrs)
Type,user = l.result(r,60)
Name,Attrs = user[0]
if hasattr(Attrs, 'has_key') and Attrs.has_key('displayName'):
  displayName = Attrs['displayName'][0]
  print displayName

sys.exit()

与运行此myusername@mydomain.co.uk password username给了我两个错误之一:

Invalid Credentials -当我输错或故意用失败来验证错误的凭据。

ldap.INVALID_CREDENTIALS:{ '信息': '80090308:LdapErr:DSID-0C090334,注释:AcceptSecurityContext错误,数据52E,vece', '递减': '无效的凭证'}

要么

ldap.OPERATIONS_ERROR:{“信息”:“00000000:LdapErr:DSID-0C090627,评论:为了执行此操作成功绑定必须在连接上完成,数据0,vece”,“递减”:“操作错误“}

我错过了正确绑定?

我收到在Fedora和窗户同样的错误。

Answer 1:

我失踪

l.set_option(ldap.OPT_REFERRALS, 0)

从初始化。



Answer 2:

如果你是开放的使用pywin32,您可以使用Win32的从Python会自动调用。 这是我们在我们的CherryPy Web服务器可以执行:

import win32security
token = win32security.LogonUser(
    username,
    domain,
    password,
    win32security.LOGON32_LOGON_NETWORK,
    win32security.LOGON32_PROVIDER_DEFAULT)
authenticated = bool(token)


Answer 3:

这为我工作,l.set_option(ldap.OPT_REFERRALS,0)=访问ActiveDirectory中的关键。 此外,我认为你应该以关闭完成脚本之前的连接添加一个“con.unbind()”。



Answer 4:

下面是对我的作品一些简单的代码。

import ldap  # run 'pip install python-ldap' to install ldap module.
conn = ldap.open("ldaphost.company.com")
conn.simple_bind_s("myuser@company.com", "mypassword")

这是基于以前的答案 。



Answer 5:

如果您已经安装了Kerberos和交谈AD,如将与,说的话,Centrify公司快速安装并运行后,你可能只使用Python的Kerveros。 例如

import kerberos
kerberos.checkPassword('joe','pizza','krbtgt/x.pizza.com','X.PIZZA.COM')`

将返回true用户“乔”在Kerberos领域X.PIZZA.COM密码“比萨”。 (典型地,我想,后者将是相同的AD域的名称)



Answer 6:

我看到@Johan滴定管您的评论对DN无固定您的问题,但我也相信,是你应该看看什么成。

鉴于你例如,在公元DN默认管理员帐户将是:CN =管理员,CN =用户,DC = MYDOMAIN,DC =合作,直流=英国 - 请尝试。



Answer 7:

我试图添加

l.set_option(ldap.OPT_REFERRALS,0)

但不是一个错误的Python只是挂起,不会对任何回应了。 也许我正在创建的搜索查询错误,什么是搜索的基本组成部分? 我使用的是相同DN的简单绑定(哦,我不得不做l.simple_bind ,而不是l.simple_bind_s ):

import ldap
local = ldap.initialize("ldap://127.0.0.1")
local.simple_bind("CN=staff,DC=mydomain,DC=com")
#my pc is not actually connected to this domain 
result_id = local.search("CN=staff,DC=mydomain,DC=com", ldap.SCOPE_SUBTREE, "cn=foobar", None)
local.set_option(ldap.OPT_REFERRALS, 0)
result_type, result_data = local.result(result_id, 0)

我使用AD LDS和实例是当前帐户注册。



Answer 8:

我有同样的问题,但它是关于密码编码

.encode('iso-8859-1')

解决了这个问题。



Answer 9:

基于优秀的LDAP3教程 :

>>> from ldap3 import Server, Connection, ALL, NTLM
>>> server = Server('server_name_or_ip', get_info=ALL)
>>> conn = Connection(server, user="user_name", password="password", auto_bind=True)
>>> conn.extend.standard.who_am_i()
>>> server.info

我没有在Python3以上,但它应该是与Python 2兼容。



Answer 10:

使用专有名称登录你的系统上。 "CN=Your user,CN=Users,DC=b2t,DC=local"应该任何LDAP系统上工作,包括AD



Answer 11:

对我来说,从改变simple_bind_s()bind()的伎俩。



文章来源: Authenticating against active directory using python + ldap