警告:ldap_bind():无法绑定到服务器:PHP和LDAP证书无效[已关闭]

qrjkbowd  于 2023-03-16  发布在  PHP
关注(0)|答案(3)|浏览(171)

**已关闭。**此问题为not reproducible or was caused by typos。当前不接受答案。

这个问题是由打字错误或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
昨天关门了。
Improve this question
我正在尝试连接到LDAP服务器以验证用户凭据。
我发现一些用户也有同样的问题,但他们的解决方案对我不起作用。
这是我用的

<?php
define('LDAP_SERVER', 'LDAP://pdc.mydomain.com');
define('LDAP_PORT', 389);
define('LDAP_TOP', 'dc=mydomain,dc=com');
if(isset($_POST['username']))
{
    if(!($ds = ldap_connect(LDAP_SERVER, LDAP_PORT)))
    {
        die ("Could not connect to mydomain domain");
    }
    $un = $_POST['username'].",".LDAP_TOP;
    //echo stripslashes($un)."<br>";
    $ldapbind = ldap_bind($ds, stripslashes($un), $_POST['password']);
    if($ldapbind)
        echo "login success";
    else
        echo "login failed";
}
?>

我试过使用“我的域名\我的用户名”和“我的用户名”。
我添加了stripslashes()函数,但这两个函数都不起作用,仍然没有结果。
我每次得到错误都是:警告:ldap_bind():无法绑定到服务器:无效凭据
任何帮助都将不胜感激
短暂性脑缺血发作

rekjcdws

rekjcdws1#

我知道这是一个很老的问题,如果你仍然需要一个答案,那么如果你在一个单独的php文件中运行这段代码会发生什么?

$username = 'hello';
$password = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.local';
$port = 389;

$connection = ldap_connect($server, $port);
if (!$connection) {
    exit('Connection failed');
}

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

$bind = @ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
    exit('Binding failed');
}

// This is where you can do your work
echo 'Hello from LDAP';

ldap_close($connection );

更多信息请访问here

qkf9rpyu

qkf9rpyu2#

我使用了以下函数:

function authenticate($username, $password){
    include 'conf/config.inc.php';

    $ldap_Userdn = getUserDN($username);

    if($ldap_Userdn!=""){
        $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
        ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

        if(ldap_bind($ldap_con, $ldap_Userdn, $password)){
            return true; 
        } else  {
            //echo "<br>Error bind checkPassword function<br>";
            return false;
        }
    } else {
        echo "Error to find user DN" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
}

function getUserDN($username){
    include 'conf/config.inc.php';

    $data = "";

    $ldap_con = ldap_connect($ldap_hostname,$ldap_port);
    ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ldap_con, LDAP_OPT_REFERRALS, 0);

    if(ldap_bind($ldap_con, $ldap_dn, $ldap_password)){
        $filter="(cn=$username)";
        $dn=$ldap_search; //even if it seems obvious I note here that the dn is just an example, you'll have to provide an OU and DC of your own

        $res = ldap_search($ldap_con, $ldap_search, $filter);
        $first = ldap_first_entry($ldap_con, $res);
        $data = ldap_get_dn($ldap_con, $first);

    } else {
        echo "<br>Error bind getUserDN function<br>" . ldap_error($ldap_con);
    }

    ldap_close($ldap_con);
    return $data;
}

这是我的config.inc.php

<?php
$ldap_hostname = "my openldap IP";
$ldap_port = "389";
$ldap_dn = "cn=Manager,dc=mydomain,dc=com";
$ldap_search = "dc=mydomain,dc=com";
$ldap_password ="my password";
?>
xwmevbvl

xwmevbvl3#

检查您的登录和密码是否正确,并在登录前添加域,见下例(HQ\login):

<?php
$login = 'HQ\student';
$password = 'MYPASS';
        
$ldap_link = ldap_connect('pdc.bc') or die("Sorry, could not connect to LDAP server.");

$ldapbind = @ldap_bind($ldap_link, $login, $password) or die ("Error trying to bind: ".ldap_error($ldap_link));
?>

相关问题