你用的是什么版本的python???。代码很旧。现在打开是初始化,不要使用simple_bind,使用simple_bind_s。
如果要在 AD 中进行删除、更改密码等操作,必须先配置 TLS 连接。http://araihan.wordpress.com/2009/10/05/windows-server-2008-active-directory-certificate-services-ad-cs/
这是一个成功的连接。
import ldap
LDAP_SERVER_EMG = "ldaps://192.168.0.250"
BIND_DN = "Administrador@emgS.local"
BIND_PASS = "xxxXXXxxxXXXxxx"
USER_BASE = "dc=emgS,dc=local"
try:
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, 0)
lcon_emg = ldap.initialize(LDAP_SERVER_EMG)
lcon_emg.simple_bind_s(BIND_DN, BIND_PASS)
except ldap.LDAPError, e:
print e
然后您可以删除和更改用户密码。
lcon_emg.passwd_s,不管用。您需要简单地更改 de unicodepwd 属性来更改 Active Directory 中的用户密码。
#firs is a good practice to create a dict of all atributes of the user
ad_u = {
'objectClass': ['top', 'person', 'organizationalPerson', 'user'],
'cn': 'User gecos or name',
'displayName': 'User gecos or name',
'User Gecos or Name',
'distinguishedName': 'user distin name',
'givenName': 'First name i guest',
'sAMAccountName': 'user_login_name',
'sn': 'middle name i guest',
#USER PRIVILEGE, SEE THE DOCUMENTATION OF AD FOR MORE INFORMATION, BECAUSE I DON'T REMEMBER :)
'userAccountControl': '514',
#user_login_name, with domain extension
'userPrincipalName': '%s@emg.local' % 'user_login_name',
'mail': 'user_login_name@emaildomainorwhatever',
'employeeID': 'unique_user_number'
}
mods = ldap.modlist.addModlist(ad_u)
try:
lcon_emg.add_s(ad_u.get('distinguishedName'),
mods)
except Exception, e:
response.update({'error_ad': 'ActiveD: Error to add user %s' % str(e)})
else:
response.update({'success_ad': 'ActiveD: Success add user'})
#HERE YOU MAKE THE utf-16-le encode password
unicode_pass = unicode('\"' + kwargs.get('cclara') + '\"', 'iso-8859-1')
password_value = unicode_pass.encode('utf-16-le')
#just change the atribute in the entry you just create
add_pass = [(ldap.MOD_REPLACE, 'unicodePwd', [password_value])]
# 512 will set user account to enabled
#change the user to enabled
mod_acct = [(ldap.MOD_REPLACE, 'userAccountControl', '512')]
try:
lcon_emg.modify_s(ad_u.get('distinguishedName'), add_pass)
except ldap.LDAPError, error_message:
response.update({'error_ad_clave': 'ActiveD: Error to gen the pass %s' % str(error_message)})
else:
response.update({'success_ad_clave': 'ActiveD: Success gen pass'})
try:
lcon_emg.modify_s(ad_u.get('distinguishedName'), mod_acct)
except ldap.LDAPError, error_message:
response.update({'error_ad_hab': 'Error to enable user %s' % str(error_message)})
else:
response.update({'success_ad_hab': 'SUccess enable user'})
lcon_emg.unbind_s()
如果您想稍后更改密码。
pad = ('"%s"' % password).encode("utf-16-le")
try:
mod_attrs = [(ldap.MOD_REPLACE, 'unicodePwd', pad),
(ldap.MOD_REPLACE,'unicodePwd',pad)]
lcon_emg.modify_s(rdnad, mod_attrs)
except Exception, e:
response.update({'error_ad': 'No se pudo cambiar la clave %s' % str(e)})
else:
response.update({'success_ad': 'Cambio exito en Active Directory'})
我希望这个答案对你有帮助