2016-05-20 34 views
-2

這是我在stackoverflow上的第一篇文章。我有點在代碼上卡住了。我一直遇到類型錯誤解碼Unicode不支持LDAP CGI密碼重置幫助我

我想要建立的是,最終用戶可以通過Web門戶重置他或她的密碼。

難道你們請幫我弄清楚如何解決這個問題? 如果您需要更多信息,請說我很樂意提供給您。

親切的問候

喬伊

! /usr/bin/python 
# _*_ coding: utf-8 _*_ 
from __future__ import unicode_literals 
import cgi 
import uuid 
import os 
import time 
import ldap 
import sys 

def index(): 
print """Content-type: text/html 
     <meta charset="UTF-8"> 
<head> 
</head> 

<h1>Reseting password web program</h1>""" 
#Declaring variables for the program for later use we could use input fields. 
PASSWORD_ATTR = "utf-8" 
username="joey hendricks" # Username for the user we want to change the password for. 
user_dn = "CN=%s,OU=Admin,OU=Assengraaf,DC=Assengraaf,DC=nl" % username 
password = 'New12345' # New password for the user we want to change the passwordfor. 
attr_name = 'mobile' 
attr_val = '12345678' 
print """<h1>"""+username,password+"""</h1>""" 
# Setting up connection tot the LDAP server we use a SSL engrypted connection to the server. 
# Without a SSL connection you could run into certain errors the avoid these we use SSL by defaul$ 
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) 
conn = ldap.initialize("ldaps://192.168.210.1:636") 
conn.set_option(ldap.OPT_REFERRALS, 0) 
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3) 
conn.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND) 
conn.set_option(ldap.OPT_X_TLS_DEMAND, True) 
conn.set_option(ldap.OPT_DEBUG_LEVEL, 255) 
conn.simple_bind_s("[email protected]","password") # Admin credentials to reset the pa$ 

if conn.compare_s(user_dn, attr_name, attr_val): 
    unicode_pass = unicode("\"" + password + "\"", "iso-8859-1") 
    password_value = unicode_pass.encode("utf-8") 
    add_pass = [(ldap.MOD_REPLACE, PASSWORD_ATTR, [password_value])] 
    try: 
     conn.modify_s(user_dn, add_pass) 
     conn.unbind_s() # Cancel the connection freeing up recources 
     print"<p1> Password set succesfull</p1>" 

    except ldap.LDAPError, e: 
     sys.stderr.write('Error setting AD password for: ' + username + '\n') 
     sys.stderr.write('Message: ' + str(e) + '\n') 
     sys.exit(1) 
     conn.unbind_s() # Cancel the connection freeing up recources 
     print"<p2>Error setting password</p2>" 

else: 
    print"<p3>No match found</p3>" 

如果 == '主要': 指數()

+0

我認爲你的密碼編碼是錯誤的。此密碼需要UTF-16編碼。看到這個鏈接:https://msdn.microsoft.com/en-us/library/cc223248.aspx –

回答

0

的後here顯示了編碼的答案在Python完整的例子正常。這些行特別感興趣:

password = "jimbo" 
password_attr = "unicodePwd" 
unicode1 = unicode("\"" + password + "\"", "iso-8859-1") 
unicode2 = unicode1.encode("utf-16-le") 
password_value = unicode2 
mods = [(ldap.MOD_REPLACE, password_attr, [password_value])] 
+0

有點遲了,但感謝您的幫助像一個魅力工作 –