python + ldap을 사용하여 Active Directory에 대한 인증
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, 주석 : AcceptSecurityContext 오류, 데이터 52e, vece', 'desc': '잘못된 자격 증명'}
또는
ldap.OPERATIONS_ERROR : { 'info': '00000000 : LdapErr : DSID-0C090627, 설명 :이 작업을 수행하려면 연결에서 성공적인 바인드를 완료해야합니다., 데이터 0, vece', 'desc': '작업 오류 '}
제대로 바인딩하려면 무엇을 놓치고 있습니까?
페도라와 창에서 동일한 오류가 발생합니다.
나는 놓치고 있었다
l.set_option(ldap.OPT_REFERRALS, 0)
초기화에서.
pywin32 사용에 개방적이라면 Python에서 Win32 호출을 사용할 수 있습니다. 이것은 CherryPy 웹 서버에서 수행하는 작업입니다.
import win32security
token = win32security.LogonUser(
username,
domain,
password,
win32security.LOGON32_LOGON_NETWORK,
win32security.LOGON32_PROVIDER_DEFAULT)
authenticated = bool(token)
그것은 나를 위해 일했습니다. l.set_option (ldap.OPT_REFERRALS, 0) 은 ActiveDirectory에 액세스하는 열쇠였습니다. 또한 스크립트를 끝내기 전에 연결을 끊으려면 "con.unbind ()"를 추가해야한다고 생각합니다.
여기 저에게 맞는 간단한 코드가 있습니다.
import ldap # run 'pip install python-ldap' to install ldap module.
conn = ldap.open("ldaphost.company.com")
conn.simple_bind_s("myuser@company.com", "mypassword")
이것은 이전 답변을 기반으로합니다 .
예를 들어 Centrify Express가 설치되어 실행되는 경우처럼 Kerberos를 설치하고 AD와 통신하는 경우 python-kerberos를 사용할 수 있습니다. 예
import kerberos
kerberos.checkPassword('joe','pizza','krbtgt/x.pizza.com','X.PIZZA.COM')`
would return True a user 'joe' has password 'pizza' in the Kerberos realm X.PIZZA.COM. (typically, I think, the latter would be the same as the name of the AD Domain)
I see your comment to @Johan Buret about the DN not fixing your problem, but I also believe that is what you should look into.
Given your example, the DN for the default administrator account in AD will be: cn=Administrator,cn=Users,dc=mydomain,dc=co,dc=uk - please try that.
I tried to add
l.set_option(ldap.OPT_REFERRALS, 0)
but instead of an error Python just hangs and won't respond to anything any more. Maybe I'm building the search query wrong, what is the Base part of the search? I'm using the same as the DN for the simple bind (oh, and I had to do l.simple_bind, instead of l.simple_bind_s):
import ldap
local = ldap.initialize("ldap://127.0.0.1")
local.simple_bind("CN=staff,DC=mydomain,DC=com")
#my pc is not actually connected to this domain
result_id = local.search("CN=staff,DC=mydomain,DC=com", ldap.SCOPE_SUBTREE, "cn=foobar", None)
local.set_option(ldap.OPT_REFERRALS, 0)
result_type, result_data = local.result(result_id, 0)
I'm using AD LDS and the instance is registered for the current account.
I had the same issue, but it was regarding the password encoding
.encode('iso-8859-1')
Solved the problem.
Based on the excellent ldap3 tutorial:
>>> from ldap3 import Server, Connection, ALL, NTLM
>>> server = Server('server_name_or_ip', get_info=ALL)
>>> conn = Connection(server, user="user_name", password="password", auto_bind=True)
>>> conn.extend.standard.who_am_i()
>>> server.info
I did the above in Python3 but it's supposed to be compatible with Python 2.
Use a Distinguished Name to log on your system."CN=Your user,CN=Users,DC=b2t,DC=local" It should work on any LDAP system, including AD
For me changing from simple_bind_s() to bind() did the trick.
참고URL : https://stackoverflow.com/questions/140439/authenticating-against-active-directory-using-python-ldap
'developer tip' 카테고리의 다른 글
| 현재 클래스를 반환 유형 주석으로 배치 (0) | 2020.09.17 |
|---|---|
| DataContractJsonSerializer와 JavaScriptSerializer의 차이점은 무엇입니까? (0) | 2020.09.17 |
| Redmine 모범 사례 (0) | 2020.09.17 |
| 가장 효율적인 스레드 안전 C ++ 로거는 무엇입니까? (0) | 2020.09.17 |
| .js 파일에 상대적인 각도 지시문 templateUrl (0) | 2020.09.17 |