使用python+ldap对active directory进行身份验证

2024-06-02 08:09:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用Python+LDAP对AD进行身份验证。我目前正在使用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: {'info': '80090308: LdapErr: DSID-0C090334, comment: AcceptSecurityContext error, data 52e, vece', 'desc': 'Invalid credentials'}

或者

ldap.OPERATIONS_ERROR: {'info': '00000000: LdapErr: DSID-0C090627, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, vece', 'desc': 'Operations error'}

我错过了什么来正确绑定?

我在软呢帽和窗户上也有同样的错误。


Tags: import身份验证basesecretserver错误sysdc
3条回答

我失踪了

l.set_option(ldap.OPT_REFERRALS, 0)

从一开始。

这对我很有用,l.set_选项(ldap.OPT_REFERRALS,0)是访问ActiveDirectory的关键。此外,我认为您应该添加一个“con.unbind()”,以便在完成脚本之前关闭连接。

如果您愿意使用pywin32,那么可以使用Python中的Win32调用。这就是我们在CherryPy web服务器中所做的:

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

相关问题 更多 >