对Active Directory中的安全组的Python LDAP身份验证

问题描述:

使用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

要限制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)")