使用PHP重置LDAP密码

时间:2010-05-14 16:41:37

标签: php

如何在PHP中重置LDAP密码?
我已经连接到LDAP服务器。

2 个答案:

答案 0 :(得分:6)

请尝试以下代码:

$dn = "uid=".$username.",dc=example,dc=com";
$newPassword = ...;
$newEntry = array('userpassword' => "{MD5}".base64_encode(pack("H*",md5($newPassword))));

if(ldap_mod_replace($ldapConnection, $dn, $newEntry))
    print "<p>succeded</p>";
else
    print "<p>failed</p>";

请参阅:

http://php.net/manual/en/function.ldap-mod-replace.php

http://logout.sh/computers/ldap/

答案 1 :(得分:0)

在寻找解决方案时,我经历了很多次……使用Symfony 5.1对我有用:

# app/config/packages/security.yaml

services:
Symfony\Component\Ldap\Ldap:
    arguments: ['@Symfony\Component\Ldap\Adapter\ExtLdap\Adapter']
Symfony\Component\Ldap\Adapter\ExtLdap\Adapter:
    arguments:
        -   host: ADServerIP
            port: 636
            encryption: ssl
            debug: true
            options:
                protocol_version: 3
                referrals: false

然后更改密码:

// Inject this
Symfony\Component\Ldap\Ldap $ldap ;

// Connect as superadmin
$ldap->bind('CN=Admin,CN=Users,DC=ADRMTW,DC=NET', 'adminN1cePassword');

$username = 'john.doe';
$newPassword = 'azerty!123';
$userPassword = mb_convert_encoding('"'.$newPassword.'"', 'utf-16le');
$query = $ldap->query('OU=client,DC=ADRMTW,DC=NET', "(&(objectclass=person)(sAMAccountName=$username))");
$result = $query->execute()->toArray();
$entry = $result[0];

$newEntry = new Entry($entry->getDn(), [
            'unicodePwd' => [$password],
        ]);
$ldap->getEntryManager()->update($newEntry);

资源:https://support.microsoft.com/kn-in/help/269190/how-to-change-a-windows-active-directory-and-lds-user-password-through