0

我有一個Windows服務器(Navision)通過Active Directory驗證提供對其API的Web訪問。
我試圖通過使用基於Linux的外部主機通過Active Directory身份驗證向Web服務器發出請求。利用Active Directory驗證的Python urllib2請求

我成功通過使用python-ldap庫進行身份驗證。

import ldap 
import urllib2 

DOMAINHOST='domain_ip_host' 

USERNAME='[email protected]' 
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 Web服務進行通信,例如,通過使用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請求?

回答

0

如果它是您想要觸發的動態導航Web服務(沒有從代碼中看到該標記,而是從標記中看到),則必須在NST上激活ntlm。 只需在CustomSettings.config中將屬性「ServicesUseNTLMAuthentication」從False更改爲True,或者僅使用Microsoft Dynamics NAV管理MMC。不要忘記在更改後重新啓動服務。

相關問題