【发布时间】:2016-09-19 13:26:48
【问题描述】:
我有一台 Windows 服务器 (Navision),通过 Active Directory 身份验证提供对其 API 的 Web 访问。
我正在尝试使用基于外部 Linux 的主机通过 Active Directory 身份验证向 Web 服务器发出请求。
我使用python-ldap 库成功验证了身份。
import ldap
import urllib2
DOMAINHOST='domain_ip_host'
USERNAME='administrator@mydomain'
PASSWORD='mycleanpassword'
URL='http://...'
conn = ldap.open(DOMAINHOST)
ldap.set_option(ldap.OPT_REFERRALS, 0)
try:
print conn.simple_bind_s(USERNAME, PASSWORD)
except ldap.INVALID_CREDENTIALS:
user_error_msg('wrong password provided')
这种情况下的输出是:
(97, [], 1, [])
表示认证成功。
我需要利用这个成功的身份验证来与 Navision 网络服务进行通信,例如通过使用urllib2 库。
req = urllib2.Request(URL)
res = urllib2.urlopen(req)
当然,由于身份验证未被利用/采用,请求失败并出现401 Unauthorized 错误。
我也尝试使用python-ntlm库:
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, URL, USERNAME, PASSWORD)
# create the NTLM authentication handler
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
# other authentication handlers
auth_basic = urllib2.HTTPBasicAuthHandler(passman)
auth_digest = urllib2.HTTPDigestAuthHandler(passman)
# disable proxies (if you want to stay within the corporate network)
proxy_handler = urllib2.ProxyHandler({})
# create and install the opener
opener = urllib2.build_opener(proxy_handler, auth_NTLM, auth_digest, auth_basic)
urllib2.install_opener(opener)
# retrieve the result
response = urllib2.urlopen(url)
print(response.read())
同样在这种情况下,提供了401 Unauthorized 错误。
如何通过针对 Active Directory 对用户进行身份验证来成功发出 Web 请求?
【问题讨论】:
标签: python active-directory windows-authentication ntlm navision