我正在嘗試使用該python-ldap庫連接到Active Directory服務器。我正在使用此鏈接中的代碼。
以下代碼正常作業:
con = ldap.initialize(uri, bytes_mode=False)
con.protocol_version = ldap.VERSION3
con.set_option(ldap.OPT_REFERRALS, 0) # required for AD authentication
con.simple_bind_s(bindDN, bindPW)
print("Authentication success!")
使用正確的憑據(在變數bindDN和bindPW中),代碼的執行可以連接到我的 AD 服務器,因此它會列印成功訊息,該訊息Authentication success!是上一個代碼片段的最后一條指令。
當我嘗試執行下面的代碼時,最后一條指令con.result3會引發 ldap.REFERRAL 例外。
# optional, but reduce the number of supported control, since only this one will be parsed
known_ldap_resp_ctrls = {
SimplePagedResultsControl.controlType: SimplePagedResultsControl,
}
# instantiate the control that will make the paged results
# it carries the page cookie (initially empty, to request the first page)
req_ctrl = SimplePagedResultsControl(
criticality=True,
size=pagesize,
cookie=''
)
# query next page, asynchronous
msgid = con.search_ext(
baseDN,
ldap.SCOPE_SUBTREE,
filterstr,
attrlist=attrlist,
serverctrls=[req_ctrl]
)
try:
con.result3(msgid, timeout=timeout, resp_ctrl_classes=known_ldap_resp_ctrls)
except ldap.REFERRAL as ex:
print("REFERRAL Exception --> " str(ex))
當引發 Exception 的 catch 塊時ldap.REFERRAL Exception,列印以下訊息:
REFERRAL Exception --> {'msgtype': 101, 'msgid': 2, 'result': 10, 'desc': 'Referral', 'ctrls': [('1.2.840.113556.1.4.319', 0, b'0\x84\x00\x00\x00\x05\x02\x01\x00\x04\x00')], 'info': 'Referral:\nldap://domain.local/DC=domain,DC=local'}
我完全堅持這個例外。
有人可以幫我找出問題所在嗎?
謝謝
如果我通過實用程式執行相同的查詢ldapsearch,它可以正常作業并且 AD 服務器發送請求的資料。
uj5u.com熱心網友回復:
抱歉,但在前面的代碼中,我在變數的設定中犯了一個錯誤,該變數baseDN具有錯誤的示例值DC=domain,DC=local。
baseDN變數由函式使用search_ext()。這是我的問題中已經顯示的代碼的一部分,它使用baseDN:
# query next page, asynchronous
msgid = con.search_ext(
baseDN,
ldap.SCOPE_SUBTREE,
filterstr,
attrlist=attrlist,
serverctrls=[req_ctrl]
)
事實上info問題中報出的例外資訊欄位的內容是:
'info': 'Referral:\nldap://domain.local/DC=domain,DC=local'
在這部分錯誤訊息中,我已經記下了這些值DC=domain,DC=local。
設定變數的正確值baseDN,LDAP 服務器將使用請求的資料回應查詢。
找到這個問題的解決方案(表現為疏忽)并不容易,因為尋找有關LDAP 參考概念的資訊我發現了例如鏈接到別名的這個oracle 檔案。LDAP referral
相反,這是一個有用的鏈接:https ://confluence.atlassian.com/crowdkb/ldap-integration-fails-with-ldap-error-code-10-658735957.html它建議我在哪里搜索問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517467.html
