2017-10-07 141 views
0

使用python-ldap對Active Directory進行身份驗證可以很好地與以下代碼一起使用,現在嘗試查找如何驗證 如果用戶屬於安全組以便成功通過身份驗證但無法弄清楚如何做到這一點。 我有這個代碼集成在燒瓶網站。對Active Directory中的安全組的Python LDAP身份驗證

這裏是我的代碼:

import ldap 
def authenticate(): 
    conn = ldap.initialize('ldap://ldap.example.com') 
    conn.protocol_version = 3 
    conn.set_option(ldap.OPT_REFERRALS, 0) 
    try: 
     username = 'user_id' 
     password = 'motdepasse' 
     user = "%[email protected]" %username 
     result = conn.simple_bind_s('user', 'password') 
    except ldap.INVALID_CREDENTIALS: 
     print "Invalid credentials" 
     return "Invalid credentials" 
    except ldap.SERVER_DOWN: 
     print "Server down" 
     return "Server down" 
    except ldap.LDAPError, e: 
     if type(e.message) == dict and e.message.has_key('desc'): 
      return "Other LDAP error: " + e.message['desc'] 
     else: 
      print "Other LDAP error: " 
      return "Other LDAP error: " + e 
    finally: 
     conn.unbind_s() 
     print "Succesfully" 
    return "Succesfully authenticated" 

authenticate() 

感謝您的幫助

+0

描述你所得到的錯誤行爲(回溯,例外,消息等)有助於瞭解你嘗試完成這項工作的程度。 – JacobIRR

+0

更具體地說,我正在尋找一種方法,我如何才能將身份驗證僅限於Active Directory中的特定安全組成員。我沒有收到上述代碼的錯誤。 – mohbar

回答

1

要限制LDAP身份驗證,我用了「search_s功能」,它發現如果通過身份驗證的用戶的一部分特定的AD組一個AD組。

conn.search_s("OU={AD Security Group},OU=group,OU=Groups,dc=twpn,dc=root,dc=domain,dc=com", ldap.SCOPE_SUBTREE, "(cn=userid)") 
相關問題