Php ldap_mod_replace返回true,但密码不更改
我想使用PHP更改LDAP目录用户的密码 绑定到LDAP后,我使用Php ldap_mod_replace返回true,但密码不更改,php,ldap,Php,Ldap,我想使用PHP更改LDAP目录用户的密码 绑定到LDAP后,我使用samaccount名称查找所需用户的dn,并检索dn: $filter="(samaccountname=desiredname.desiredname)"; $result = ldap_search($lh, $personnel_base, $filter) or die(ldap_error($lh)); //$data = ldap_get_entries($lh, $result); $entry = ldap_f
samaccount
名称查找所需用户的dn,并检索dn
:
$filter="(samaccountname=desiredname.desiredname)";
$result = ldap_search($lh, $personnel_base, $filter) or die(ldap_error($lh));
//$data = ldap_get_entries($lh, $result);
$entry = ldap_first_entry($lh, $result);
$atribute = ldap_get_attributes($lh, $entry);
然后我使用ldap_mode_replace更改密码:
$newpass = "Cevadetest123#!";
ldap_mod_replace($lh, $dn, array('userpassword' => "{MD5}".base64_encode(pack("H*",md5($newpass) ) ) ) ) or die(ldap_error($lh));
echo "Password changed!";
虽然我更改了密码代码>输出,密码保持不变
有什么建议吗
编辑:我刚刚注意到属性
userpassword
确实发生了变化,但要通过LDAP登录,我必须使用旧密码!这是什么意思?我找到了答案。首先,我必须更改的字段是unicodePwd
,它无法读取-只能修改。要写入此字段,必须首先与LDAP建立安全连接。因此,主机名为:ldaps://hostname.something.local
下一个重要步骤是在写入字段之前加密密码:
$newpassword="HelloWorld123";
$newpassword = "\"".$newpassword."\"";
$newPass = mb_convert_encoding($newpassword, 'UTF_16LE')
您可以找到完整的代码
我会将其粘贴到下面,以防链接出现问题:
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
$ldapconn = ldap_connect('ldaps://127.0.0.1', 636);
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
$ldapuser="ldapuser";
$ldappwd="*****";
// search for user
ldap_bind($ldapconn, "CN=$ldapuser,CN=Users,DC=my,DC=company,DC=example", $ldappwd);
$res_id = ldap_search( $ldapconn, "CN=Users,DC=my,DC=company,DC=example", "sAMAccountName=$username");
if ($res_id) {
$entry_id = ldap_first_entry($ldapconn, $res_id);
if($entry_id){
$user_dn = ldap_get_dn($ldapconn, $entry_id);
if ($user_dn) {
$ldapbind = ldap_bind($ldapconn, $user_dn, $oldpassword);
// check if the old password allows a successfull login
if($ldapbind) {
if(strcmp($newpassword, $newpassword2)==0){
// create the unicode password
$newpassword = "\"" . $newpassword . "\"";
$newPass = mb_convert_encoding($newpassword, "UTF-16LE");
//rebind as admin to change the password
ldap_bind($ldapconn, "CN=$ldapuser,CN=Users,DC=my,DC=company,DC=example", $ldappwd);
$pwdarr = array('unicodePwd' => $newPass);
if(ldap_mod_replace ($ldapconn, $user_dn, $pwdarr)) {
print "<p class='success'>Change password succeded.</p>\n";
} else {
print "<p class='error'>Change password failed.</p>\n";
}
}else{
print "<p class='error'>New password must be entered the same way twice.</p>\n";
}
}else{
print "<p class='error'>Wrong user name or password.</p>\n";
}
} else {
print "<p class='error'>Couldn't load user data.</p>\n";
}
} else {
print "<p class='error'>Couldn't find user data.</p>\n";
}
} else {
print "<p class='error'>Username was not found.</p>\n";
}
if(ldap_error($ldapconn)!="Success"){
print "<p class='error'>LDAP Error:<br />\n";
var_dump(ldap_error($ldapconn));
print "</p>\n";
}
@ldap_close($ldapconn);
ldap\u set\u选项(NULL,ldap\u OPT\u DEBUG\u级别,7);
$ldapconn=ldap\u connect('ldaps://127.0.0.1', 636);
ldap_设置_选项($ldapconn,ldap_选项_协议_版本,3);
ldap\u set\u选项($ldapconn,ldap\u OPT\u references,0);
$ldapuser=“ldapuser”;
$ldappwd=“*******”;
//搜索用户
ldap_bind($ldapconn,“CN=ldapuser,CN=Users,DC=my,DC=company,DC=example”,$ldappwd);
$res_id=ldap_search($ldapconn,“CN=Users,DC=my,DC=company,DC=example”,“sAMAccountName=$username”);
如果($res_id){
$entry\u id=ldap\u first\u条目($ldapconn,$res\u id);
如果($entry\U id){
$user\u dn=ldap\u get\u dn($ldapconn,$entry\u id);
如果($user\u dn){
$ldapbind=ldap\u bind($ldapconn,$user\u dn,$oldpassword);
//检查旧密码是否允许成功登录
如果($ldapbind){
如果(strcmp($newpassword,$newpassword2)==0{
//创建unicode密码
$newpassword=“\”.$newpassword。“\”;
$newPass=mb_convert_编码($newpassword,“UTF-16LE”);
//以管理员身份重新绑定以更改密码
ldap_bind($ldapconn,“CN=ldapuser,CN=Users,DC=my,DC=company,DC=example”,$ldappwd);
$pwdar=array('unicodePwd'=>$newPass);
如果(ldap_mod_replace($ldapconn,$user_dn,$pwdar)){
打印“更改密码成功。
\n”;
}否则{
打印“更改密码失败。
\n”;
}
}否则{
打印“新密码必须以相同方式输入两次。
\n”;
}
}否则{
打印“错误的用户名或密码。
\n”;
}
}否则{
打印“无法加载用户数据。
\n”;
}
}否则{
打印“找不到用户数据。
\n”;
}
}否则{
打印“未找到用户名。
\n”;
}
如果(ldap_错误($ldapconn)!=“成功”){
打印“LDAP错误:
\n”;
var_转储(ldap_错误($ldapconn));
打印“
\n”;
}
@ldap_close($ldapconn);
谁实际绑定到LDAP?绑定到LDAP的用户是否被允许读取相关用户的密码?@heiglandreas我找到了答案并发布了它