简体   繁体   English

如何在PHP上使用LDAPS连接到Active Directory?

[英]How can I connect to Active Directory using LDAPS on PHP?

I was wondering how to connect to my Active Directory Domain Controller using LDAPS in PHP on another windows server. 我想知道如何在另一台Windows服务器上使用PHP LDAPS连接到我的Active Directory域控制器。 I have exported the root certificate and the server certificate and put the root in my trusted root store and the server authentication in my personal certificates in my windows certificate store. 我已经导出了根证书和服务器证书,并将根放在我的受信任的根存储中,而服务器身份验证则放在了Windows证书存储中的个人证书中。 When I try to connect using port 389 it's fine, but when I try to connect using port 636 I get an error. 当我尝试使用port 389连接时很好,但是当我尝试使用port 636连接时,出现错误。

 // LDAP variables <br>
 $ldap_host = "myhost";   // your ldap servers<br>
 $ldap_port = 636;          // your ldap server's port number<br>
 $base_dn = "OU=Users,OU=domain,DC=example,DC=local";<br>

// Connecting to LDAP<br>
$connect = ldap_connect( $ldap_host, $ldap_port)<br>
or exit(">>Could not connect to LDAP server<<");<br>
echo "Connected to $ldap_host";<br>

ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);<br>
$ldap_user  = "CN=UserName,OU=No Policy,DC=example,DC=local";<br>
$ldap_pass = "Password";<br>
// verify binding<br>
$bind = ldap_bind($connect, $ldap_user, $ldap_pass)<br>
or exit(">>Could not bind to $ldap_host<<" . ldap_error($connnect) );<br>

The output I get is "Connected to myhost Could not bind to myhost. 我得到的输出是“连接到myhost无法绑定到myhost。
When using port 389 I get "connected to myhost" 使用端口389时,我“连接到myhost”

As stated in the docs you need to use an LDAP-URI to connect via LDAPS. 文档中所述,您需要使用LDAP-URI通过LDAPS连接。

In your case that would look like this: 您的情况如下所示:

ldap_connect('ldaps://'. $ldap_host. ':'. $ldap_port);

I was able to connect to my server using ldaps on PHP using the following method. 我可以使用以下方法在PHP上使用ldaps连接到我的服务器。

$connect = ldap_connect('ldaps://'. $ldap_host. ':'. $ldap_port)

I also had to create these folders C:\\openldap\\sysconf and then put a text document named ldap.conf into it. 我还必须创建这些文件夹C:\\ openldap \\ sysconf,然后将名为ldap.conf的文本文档放入其中。
I then edited ldap.conf and put in TLS_REQCERT never 然后,我编辑了ldap.conf并放入了TLS_REQCERT
This worked for connecting through LDAPS. 这适用于通过LDAPS连接。
The correct way to do it, is to download cacert.pem from here: 正确的方法是从此处下载cacert.pem:
https://curl.haxx.se/docs/caextract.html Then add your server hash onto the bottom of this cert. https://curl.haxx.se/docs/caextract.html,然后将服务器哈希添加到此证书的底部。
Lastly edit ldap.conf to say TLS_CACERT \\path\\to\\cert\\cacert.pem and comment out the TLS_REQCERT comment from above. 最后,编辑ldap.conf以说出TLS_CACERT \\ path \\ to \\ cert \\ cacert.pem,并从上方注释掉TLS_REQCERT注释。
Then restart apache/nginx/etc and you should be able to connect using LDAPS. 然后重新启动apache / nginx / etc,您应该能够使用LDAPS进行连接。

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

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