好的,我有一些关于LDAP的工作。如果我的代码对你有帮助的话,我会给你的。首先,我将cert放入django project。在BASE_DIR = Djando base dir
def returnPaths():
return {'dn_full_path': 'CN=Person,CN=Schema,CN=your_con,DC=your_dc,DC=your_dc',
'cert': 'path_to_cert_file',
'host': 'ldaps://x.x.x.:636'
}
class LdapBackend(object):
@classmethod
def authenticate(cls, username=None, password=None):
try:
setup = returnPaths()
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, BASE_DIR + setup['cert'])
domain = 'your_doman'
l = ldap.initialize(setup['host'])
l.set_option(ldap.OPT_REFERRALS, 0)
l.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
l.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND)
l.set_option(ldap.OPT_X_TLS_DEMAND, True)
try:
l.simple_bind_s('%s@%s' % (str(username.encode('utf-8')), domain),
password.encode('utf-8'))
return True
except ldap.INVALID_CREDENTIALS as e:
return False
except Exception as e:
return False
except ldap.INVALID_CREDENTIALS as e:
return False