4

我正在尝试使用 PHP 使用 LDAP 身份验证。
下面是我的代码:

<?php

$ldaphost = 'ldap://ldapServer';
$ldapport = 389;

$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
//ldap_set_option($ds, LDAP_OPT_DEBUG_LEVEL, 7);
if ($ds) 
{
    $username = "testuser@domain.com";
    $upasswd = "testpass";

    $ldapbind = ldap_bind($ds, $username, $upasswd);


    if ($ldapbind) 
        {print "Congratulations! $username is authenticated.";}
    else 
        {print "Access Denied!";}


}
?>

但它会引发以下错误:

PHP 警告:: ldap_bind()无法绑定到服务器:无法联系 LDAP 服务器

知道如何解决它吗?

注意:ldap.config当我在某个论坛上遇到这个术语时,我们是否需要在某个地方归档。我在我的机器上没有看到任何这样的文件。我php_ldap.dll在 ext 文件夹中并使用Windows.

4

5 回答 5

2

绑定时,您绑定的不是用户名,而是DN

您的 $username 变量应如下所示:

$username = 'uid=testuser,ou=People,dc=domain,dc=com';
于 2012-09-04T09:06:14.333 回答
1

我猜ldap_connect()不需要协议,所以这个天真的补丁应该可以解决你的问题:

--- ldap.php.bak    2012-09-04 10:52:29.563203493 +0200
+++ ldap.php    2012-09-04 10:52:46.807203766 +0200
@@ -1,6 +1,6 @@
 <?php

-$ldaphost = 'ldap://ldapServer';
+$ldaphost = 'ldapServer';
 $ldapport = 389;

 $ds = ldap_connect($ldaphost, $ldapport)

查看官方文档中的基本示例

于 2012-09-04T08:54:22.780 回答
0

正如 Minras 所说,您使用了错误的凭据进行绑定。尝试这样的事情:

$ldaprdn  = 'cn=binder,dc=domain,dc=com';     // ldap rdn or dn or proxy agent or admin
$ldappass = 'password';  // associated password

// connect to ldap server
$ldapconn = ldap_connect("54.85.xx.xx")
        or die("Could not connect to LDAP server.");

// Set some ldap options for talking to 
ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);

if ($ldapconn) {

        // binding to ldap server
        $ldapbind = @ldap_bind($ldapconn, $ldaprdn, $ldappass);

        // verify binding
        if ($ldapbind) {
            echo "Done..\n</h1>";
        } else {
            echo "Damn you LDAP...\n";
        }

}
于 2014-02-21T14:18:42.460 回答
0

我们在本地网络中证明了它并且运行良好。例如,如果您将 Ldap 与 Zentyal go 一起使用https://serveriporname/Users/Composite/Settings,然后在“用户 DN”中选择它为您提供的选项,那么您将使用我们将调用的地址$userdns,您可以证明以下代码

<?php
       //The variables are implicit

    $ad = ldap_connect($ldap_server) ;  //Ex: 10.0.0.1
    ldap_set_option($ad, LDAP_OPT_PROTOCOL_VERSION, 3) ; 

       //Using the provided user and password to login into LDAP server.
       //For the dc, normally will be the domain. 

        $sr=ldap_search($ad, $userdns, "cn=*usuario*");

        $info = ldap_get_entries($ad, $sr);

       for ($i=0; $i<$info["count"]; $i++) {
          /*Print out the user information here. If you rather to request by other field than cn take its name from here*/
          print_r($info[$i]);
          echo "<p><hr/></p>";
        }
       $ds = ldap_bind($ad,"uid=$ldap_user,$userdns",$ldap_pass);
       if($ds){
      echo "<h4>$ldap_user connect to LDAP server \"$ldap_domain\"</h4>";
       } else {
         echo "<h4>Unable to connect to LDAP server</h4>";
       }  
       ldap_close($ad);
?>
于 2014-01-24T19:49:26.233 回答
0

我不确定您是否仍然需要答案,但我想根据我的经验添加一些内容。

$username   = 'bentcoder';
$password   = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.com';
$port       = 389;

$ldap_connection = ldap_connect($server, $port);

if (! $ldap_connection)
{
    echo '<p>LDAP SERVER CONNECTION FAILED</p>';
    exit;
}

// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

$ldap_bind = @ldap_bind($ldap_connection, $username.$domain, $password);

if (! $ldap_bind)
{
    echo '<p>LDAP BINDING FAILED</p>';
    exit;
}

// You can work now!!!

ldap_close($ldap_connection);

笔记:

  • php_ldap应在 php.ini 中启用扩展。[扩展=php_ldap.dll]
  • [192.168.32.4]上面的IP地址可以换成[yourdomain.com]
  • 如果您使用的是 Wamp,那么您可能会遇到奇怪的问题,只要记住在 1.9 版下一切正常,但在以上版本中则不行。

参考:

于 2015-06-20T21:20:08.403 回答