【问题标题】:Python urllib2 request exploiting Active Directory authenticationPython urllib2 请求利用 Active Directory 身份验证
【发布时间】: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


    【解决方案1】:

    如果它是您想要触发的 Dynamics NAV Web 服务(没有从代码中看到,而是从标签中看到),您必须在 NST 上激活 ntlm。 只需在 CustomSettings.config 中将属性“ServicesUseNTLMAuthentication”从 False 更改为 True,或者仅使用 Microsoft Dynamics NAV Administration MMC。改完后别忘了重启服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 2017-12-09
      • 2014-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多