我刚开始使用LDAP和Windows Server 2012。我已经设法让我的PHP代码绑定到使用管理员帐户的活动目录,但我似乎可以使用ldap_add函数创建一个新用户。
我正在运行Windows Server 2012 R2和IIS 8。我已经安装了具有企业根证书的证书颁发机构,并且可以使用带有SSL连接的ldp.exe连接到AD。
我已经通过谷歌以及"可能的重复线程"查看了,但在我一直在寻找的过去几个小时里,他们都没有提供给我一个有效的答案。
当我使用以下PHP代码:
<?php
$ldaprdn = 'netclass'Administrator';
$ldappass = 'password here';
$ldapconn = ldap_connect('ldap://server.netclass.co.uk')
or die("Could not connect to LDAP server.");
if ($ldapconn) {
// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
// verify binding
if ($ldapbind) {
echo "LDAP bind successful...";
} else {
echo "LDAP bind failed...";
}
// prepare data
$info["givenname"] = "Ronnie Brown";
$info["samaccountname"] = "br01";
$info["objectclass"] = "person";
// add data to directory
$r = ldap_add($ldapconn, "cn=Ronnie,dc=netclass,dc=co,dc=uk", $info); //Line 28
}
?>
我得到了输出:
LDAP bind successful...
Warning: ldap_add(): Add: Server is unwilling to perform in C:'inetpub'wwwroot'adduser.php on line 28
我试图通过更改启用SSL:
$ldapconn = ldap_connect('ldap://server.netclass.co.uk')
$ldapconn = ldap_connect('ldaps://server.netclass.co.uk')
但是我得到以下错误:
Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server in C:'inetpub'wwwroot'adduser.php on line 12
LDAP bind failed...
Warning: ldap_add(): Add: Can't contact LDAP server in C:'inetpub'wwwroot'adduser.php on line 28
我今天终于算出答案了。
我在/etc/ldap/ldap.conf
中添加了以下行TLS_REQCERT never
在此之后,我能够连接,但仍然得到错误信息:
Server Unwilling to Perform
这是因为我试图将密码设置为纯文本,如:
$ldaprecord["unicodepwd"] = 'MyPassword1234'
你需要先对它进行编码,所以一旦我把代码改成这样,它就可以工作了:
## Create Unicode password
## Assumes that given password is in UTF-8 encoding!
## Adjust it to the actual encoding of the password
$pwdtxt = "MyPassword1234";
$newPassword = '"' . $pwdtxt . '"';
$newPass = iconv( 'UTF-8', 'UTF-16LE', $newPassword );
$ldaprecord["unicodepwd"] = $newPassw;
希望这能帮助到一些人!