【问题标题】:how to add an Active directory authenfication password with PHP?如何使用 PHP 添加 Active Directory 身份验证密码?
【发布时间】:2019-10-02 07:28:24
【问题描述】:

我来找你是因为我的 Active Directory 服务器有点问题,确实我可以连接以获取我的用户...当我使用 AD 软件添加用户时,我为他分配了一个密码,并且这个密码已启用在PHP中使用表单进行身份验证,没有任何异常。

问题是当我直接使用 PHP 添加用户时,我使用此代码

 $ldapconn=ldap_connect("adress");
 $ldapbind=ldap_bind($ldapconn, "local", "test");
 $cn = $info["cn"][0] = "test test ";
 $info["sn"][0] ="test ";
 $info["givenname"][0] ="test ";
 $info["displayname"][0] ="test test ";
 $info["name"][0] ="test test ";
 $info["userprincipalname"][0] = "test @test .test ";
 $info["samaccountname"][0] = "ttest ";

 $info["objectClass"][0]="top";
 $info["objectClass"][1]="person";
 $info["objectClass"][2]="organizationalPerson";
 $info["objectClass"][3]="user";
 $info["objectCategory"][0] ="CN=Person,CN=Schema,CN=Configuration,DC=test 
 ,DC=test ";
 $info['userPassword'][0] = "test ";

// add entries
$r = ldap_add($ldapconn,"CN=".$cn.",OU=Utilisateurs,DC=test ,DC=test ", $info);

我的用户已正确添加但未完成 php 身份验证,我的用户没有身份验证密码,但应该用于其他用途的密码,我在网上看到 $info['userPassword'] 允许创建一个不可用于身份验证的密码。请有人拥有确切的属性才能创建此密码。

提前感谢您的帮助。

我也尝试使用属性 $info['unicodePwd'] 但我收到一条错误消息 ” 添加:服务器不愿意执行”。

【问题讨论】:

    标签: php active-directory ldap


    【解决方案1】:

    userPassword 属性有时有效,但unicodePwd 是真正的属性。

    有几个注意事项:

    1. 您必须通过加密连接连接到 AD。这意味着 LDAP over SSL (LDAPS)。因此,在您拨打ldap_connect 时,您需要使用ldaps://example.com。由于服务器发送的 SSL 证书需要被您的应用程序信任,因此这可能会引发大量蠕虫。文档页面上有some comments 可以提供帮助。

    2. 你分配给unicodePwd的值的格式必须是:

    在 UTF-16 编码的 Unicode 字符串中指定,其中包含用引号括起来的密码,根据 Object(Replica-Link) 语法已将其 BER 编码为八位字节字符串。

    documentation page for ldap-mod-replace上也有这样的例子:

    $newPassword = "MyPassword";
    $newPassword = "\"" . $newPassword . "\"";
    
    $len = strlen($newPassword);
    for ($i = 0; $i < $len; $i++)
            $newPassw .= "{$newPassword{$i}}\000";
    
    $newPassword = $newPassw;
    $userdata["unicodepwd"] = $newPassword;
    $result = ldap_mod_replace($ad, $userDn, $userdata);
    if ($result) echo "User modified!" ;
    else echo "There was a problem!";
    

    您可能必须先创建帐户后使用ldap_mod_replace 设置密码(而不是在创建帐户时使用$info['unicodepwd'][0])。根据我的经验,我总是必须先创建帐户,然后设置密码。

    但由于一开始没有密码,帐户通常会被禁用(取决于您的政策)。因此,您可能必须在设置密码后将userAccountControl 属性设置为512 (NORMAL_ACCOUNT) 才能启用它。

    【讨论】:

      【解决方案2】:

      感谢您的回答,我尝试了您的解决方案,没有放置“[unicodepwd]”并在之后使用“ldap_mod_replace”,但我收到一条错误消息“服务器不愿意执行”。

      我认为我的服务器是以特定方式配置的。

      我找到了另一个解决方案,我使用“Adldap2”工具 (https://adldap2.github.io/Adldap2/#/?id=what-is-adldap2),其中包含将 LDAP 与 PHP 一起使用所需的所有功能。

      我推荐给任何对 Active Directory 有困难的人。 例如,我的问题是用这段代码解决的:

      enter code include __DIR__ . '/vendor/autoload.php';
      $config = [
          // Mandatory Configuration Options
          'hosts'            => ['testServer'],
          'base_dn'          => 'dc=test,dc=test',
          'username'         => 'test@test.test',
          'password'         => 'test',
          'account_suffix'   => '@test.test',
          'port'             => 636,
          'use_ssl'          => true,
      ];
      $ad = new Adldap\Adldap();
      $connectionName = 'my-connection';
      $ad->addProvider($config, $connectionName);
      try {
          $provider = $ad->connect($connectionName);
          echo"Connection ok <br>" ;
          // Great, we're connected!
      } catch (Adldap\Auth\BindException $e) {
          // Failed to connect.
          echo"Connection failed<br>" ;
      }
      
          $user = $provider->make()->user([
          'cn' => 'test test ',
          'userprincipalname' => 'test@test.test',
          'accountExpires'=> 132188680854770000
      ]);
      $user->setDn("CN=test tes ,OU=test ,DC=test,DC=test");
      $dn = $user->getDn();
      $user->setDisplayName('test test ');
      $user->setAccountName('ttest');
      $user->setFirstName('test');
      $user->setLastName('test');
      $attribut = $user->getAttributes();
      var_dump($attribut);
      $user->setEmail('test@test.test');
      // Save the new user.
      // Enable the new user (using user account control).
      if ($user->save()) {
          // Enable the new user (using user account control).
          $user->setUserAccountControl(66048);
      
          // Set new user password
          $user->setPassword('test');
      
          // Save the user.
          if($user->save()) {
              // The password was saved successfully.
          }
      }
      

      希望这能有所帮助:)

      【讨论】:

        猜你喜欢
        • 2015-10-18
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多