简体   繁体   English

使用 PHP 将用户密码添加到 AD

[英]Adding user password to an AD with PHP

My API (wrote in PHP) needs to register a new user in an AD.我的 API(用 PHP 编写)需要在 AD 中注册一个新用户。 It's like the userPassword attribute does not set the password of the user (so he cannot log in).这就像userPassword属性没有设置用户的密码(因此他无法登录)。

Things I've tried :我尝试过的事情:

  • Send the password in plain text : nOK以纯文本形式发送密码:nOK
  • Send the password with Base64 encoding : nOK使用 Base64 编码发送密码:nOK

I've read an article ( see here ) about unicodePwd and the use of LDAPS, but I'm not really sure how to implement this.我读过一篇关于unicodePwd和 LDAPS 使用的文章( 见这里),但我不确定如何实现这一点。

I'm actually working with a non-TLS LDAP connection (it's on a local network so I don't need it) on Win2k16 (latest version).我实际上在 Win2k16(最新版本)上使用非 TLS LDAP 连接(它在本地网络上,所以我不需要它)。

Domain is secureconnect.online (in my code it's .local but don't mind about it).域是secureconnect.online (在我的代码中它是.local但不介意它)。

Here's my code :这是我的代码:

public function addUser()
{
// LDAP variables
$ldap_username = $this->ldap_creds['username'];
$ldap_password = $this->ldap_creds['password'];
$ldapuri = $this->ldap_creds['uri'];

// LDAP connection
$link_id = ldap_connect($ldapuri);
if ($link_id) {

    ldap_set_option($link_id, LDAP_OPT_PROTOCOL_VERSION, 3);

    ldap_bind($link_id, $ldap_username, $ldap_password);

    $lastname = strtolower($this->validFormData[0]);
    $firstname = strtolower($this->validFormData[1]);

    $username = $firstname . $lastname;
    $display_name = ucwords($firstname) . " " . ucwords($lastname);

    $unhashed_pass = $this->validFormData[8];
    $encoded_newPassword = "{SHA}" . base64_encode(pack("H*", sha1($unhashed_pass)));

    $adduserAD["cn"] = $username;
    $adduserAD["givenname"] = ucwords($firstname);
    $adduserAD["sn"] = ucwords($lastname);
    $adduserAD["sAMAccountName"] = $username;
    $adduserAD['userPrincipalName'] = $this->validFormData[2];
    $adduserAD["objectClass"] = "user";
    $adduserAD["displayname"] = $display_name;
    $adduserAD["userPassword"] = $encoded_newPassword;
    $adduserAD["userAccountControl"] = "544";
    $adduserAD['postalCode'] = $this->validFormData[5];
    // Add city
    $adduserAD['l'] = $this->validFormData[6];
    // Add street address
    $adduserAD['streetAddress'] = $this->validFormData[4];

    $dn = 'OU=Users-VPN,DC=secureconnect,DC=local';
    $base_dn = 'cn=' . $adduserAD['cn'] . ',' . $dn;

    $req = ldap_add($link_id, $base_dn, $adduserAD);
    if ($req) {
        $this->result = $username;
        ldap_close($link_id);
    } else {
        $this->result = '{"error":"Contact Administrator"}';
    }
} else {
    $this->result = '{"error":"Cannot Connect To Ldap Server"}';
}
return $this->result;
}

Thank's in advance !提前致谢 !

EDIT : So, I've installed an AD LDS with a trusted root certificate.编辑:所以,我已经安装了一个带有受信任根证书的 AD LDS。 Now when I'm trying to connect with TLS to the server through my API, I'm stuck at this error :现在,当我尝试通过我的 API 将 TLS 连接到服务器时,我遇到了这个错误:

Warning: ldap_start_tls(): Unable to start TLS: Can't contact LDAP server

Here's the code :这是代码:

/**
 * Method used to add and user to an LDAP annuary.
 * @return bool|string
 */
public function addUser()
{
    // LDAP variables
    $ldap_username = $this->ldap_creds['username'];
    $ldap_password = $this->ldap_creds['password'];
    $ldapuri = $this->ldap_creds['uri'];

    // Connexion LDAP
    $link_id = ldap_connect($ldapuri);
    if ($link_id) {

        ldap_set_option($link_id, LDAP_OPT_PROTOCOL_VERSION, 3);

        ldap_start_tls($link_id);
        echo "yeet";

        ldap_bind($link_id, $ldap_username, $ldap_password);

Everything beyond this snippet is the same as above.此代码段之外的所有内容都与上述相同。

What should I do ?我该怎么办 ? Do I need to import the certificate to the Web server ?我需要将证书导入到 Web 服务器吗?

We have some documentation that explains the process and requirements for using LDAP for Setting and Changing Microsoft Active Directory Passwords我们有一些文档解释了使用 LDAP 设置和更改 Microsoft Active Directory 密码的过程和要求

And there is a sample in JAVA that shows how we have performed the operation.并且在 JAVA 中有一个示例,显示了我们如何执行操作。 Usually it works out best to create the user and then set the UnicodePwd value.通常最好创建用户然后设置 UnicodePwd 值。 We are not real sure why this is the case but issues have been encountered when attempting to both in one operation.我们不确定为什么会出现这种情况,但在尝试在一项操作中同时进行时遇到了问题。

Additionally, often depending on your Microsoft Active Directory settings users are created as disabled and may need to be enabled to be effective.此外,通常根据您的 Microsoft Active Directory 设置,用户被创建为禁用状态,可能需要启用才能生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM