【问题标题】:Changing (not resetting) a users password using PHP LDAP使用 PHP LDAP 更改(而不是重置)用户密码
【发布时间】:2019-08-14 10:02:10
【问题描述】:

我正在尝试使用 php-ldap 更改 Microsoft 活动目录中的用户密码。问题是当尝试使用ldap_mod_replace 更改密码时,它并没有更改而是重置密码,这不是我想要的,因为我的用户不允许重置自己的密码。

活动目录基于 Microsoft 服务器 2016,我的应用程序在使用 PHP 7.2 的 IIS 网络服务器上运行。

// open LDAP connection 
$ldap_connection = $this->connectToActiveDirectory();

if (@ldap_bind($ldap_connection, $this->ldap_username . '@' . env('LDAP_DOMAIN', 'localhost'), $request->oldPassword)) {
    // LDAP connection established

    $dn = $request->userdn; // distinguished name of user

    /* 
     The DC requires that the password value be specified in a UTF-16 encoded Unicode
     string containing the password surrounded by quotation marks, which has been BER-encoded
     as an octet string per the Object(Replica-Link) syntax.
     */
    $newPassword = "\"" .$request->password. "\"";
    $utf16Password = ""; // converted password
    $passwordLength = strlen($newPassword);
    for ($i = 0; $i < $passwordLength; $i++) {
        $utf16Password .= "{$newPassword{$i}}\000";
    }

    $passwordEntry = array('unicodePwd' => $utf16Password);

    // Set new password
    if(@ldap_mod_replace($ldap_connection, $dn, $passwordEntry)) {
        // Successful   
    } else {
        // Error, probably not enough permissions

        return back(); // Redirect user to previous page
    }

    ldap_unbind($ldap_connection); // Close LDAP connection

    return redirect('/logout'); // Redirect user to logout
}

我想更改密码而不是重置密码,但我找不到解决方案。也许你们中的一些人遇到过这个问题,我很感谢您的帮助!

【问题讨论】:

  • 更改和重置有什么区别?
  • @GiacomoM 当您重置密码时,您不必知道旧密码,例如管理员会重置密码。更改密码时,您必须知道旧密码。授予用户重置自己密码的权限的问题在于,当他与他的用户一起登录并且他的计算机处于解锁状态时,任何人都可以更改他的密码而无需知道旧密码。

标签: php laravel active-directory ldap


【解决方案1】:

根据documentation for the unicodePwd attribute,这就是你的做法:

如果修改请求包含一个删除操作,其中包含 unicodePwd 的值 Vdel,然后是一个包含 unicodePwd 的值 Vadd 的添加操作,则服务器认为该请求是更改密码的请求。 ... Vdel 是旧密码,而 Vadd 是新密码。

简而言之,您需要在一个 LDAP 请求中删除该值并添加该值。在 PHP 中,这意味着使用 ldap_modify_batch 函数。事实上,在那篇文档中,there is an example 讲述了如何更改密码:

<?php
function adifyPw($pw)
{
    return iconv("UTF-8", "UTF-16LE", '"' . $pw . '"');
}

$dn = "cn=Jack Smith-Jones,ou=Wizards,dc=ad,dc=example,dc=com";
$modifs = [
    [
        "attrib"  => "unicodePwd",
        "modtype" => LDAP_MODIFY_BATCH_REMOVE,
        "values"  => [adifyPw("Tr0ub4dor&3")],
    ],
    [
        "attrib"  => "unicodePwd",
        "modtype" => LDAP_MODIFY_BATCH_ADD,
        "values"  => [adifyPw("correct horse battery staple")],
    ],
];
ldap_modify_batch($connection, $dn, $modifs);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2020-04-08
    • 2012-12-10
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多