2016-05-20 6 views
-2

これは私の最初の投稿です。私はいくつかのコードでちょっとここにこだわっています。私はタイプエラーのデコードを続けています。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

ちょっと遅かったですが、おかげさまでお世話になりました。 –