我正在使用Active Directory,我想更改用户的密码。我拥有所有权限,并获得Active Directory的所有用户的信息,问题是更改unicodePwd字段。
以下是我使用的代码:
<?php
function create_ldap_connection() {
$ip = "10.60.0.12";
$ldaps_url = "ldap://$ip";
$port = 636;
$ldap_conn = ldap_connect($ldaps_url, $port)
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_conn, LDAP_OPT_REFERRALS, 0);
$password = "administrador";
$binddn = "CN=Administrador,CN=Users,DC=uart,DC=edu,DC=cu";
$result = ldap_bind($ldap_conn, $binddn, $password) or die("
Error: Couldn't bind to server using provided credentials!");
if ($result) {
return $ldap_conn;
} else {
die("
Error: Couldn't bind to server with supplied credentials!");
}
}
function get_user_dn($ldap_conn, $user_name) {
$basedn = "DC=uart,DC=edu,DC=cu";
$searchResults = ldap_search($ldap_conn, $basedn, "(samaccountname=$user_name)");
if (!is_resource($searchResults))
die('Error in search results.');
/* Get the first entry from the searched result */
$entry = ldap_first_entry($ldap_conn, $searchResults);
return ldap_get_dn($ldap_conn, $entry);
}
function pwd_encryption($newPassword) {
$newpass = "";
$newPassword = "\"" . $newPassword . "\"";
$len = strlen($newPassword);
for ($i = 0; $i < $len; $i++) {
$newpass .= "{$newPassword{$i}}\000";
}
$userdata["unicodePwd"] = $newpass;
return $userdata;
}
$user_name = "ybolmey"; //Dont remove parentheses brackets
$user_password = "jaciel2014*";
$ldap_conn = create_ldap_connection();
$userDn = get_user_dn($ldap_conn, $user_name);
$userdata = pwd_encryption($user_password);
$result = ldap_mod_replace($ldap_conn, $userDn, $userdata);
if ($result)
echo "Your password has been changed!";
ldap_close($ldap_conn);
?>
请帮帮我!!感谢
答案 0 :(得分:0)
不确定是否已修复此问题,但您需要使用LDAPS才能更改unicodePwd字段。
所以这个: $ ldaps_url =“ldap:// $ ip”;
需要: $ ldaps_url =“ldaps:// $ ip”;