警告:ldap_bind():无法绑定到服务器:PHP 和 LDAP 凭据无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: ldap_bind(): Unable to bind to server: Invalid credentials PHP and LDAP
提问by user1154725
I'm trying to connect to an LDAP server to authenticate user credentials.
我正在尝试连接到 LDAP 服务器以验证用户凭据。
I've found a few users with this same issue but their solutions did not work for me.
我发现一些用户遇到了同样的问题,但他们的解决方案对我不起作用。
here's what I'm using:
这是我正在使用的:
<?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";
}
?>
I've tried using "mydomain\myusername" and just "myusername".
我试过使用“mydomain\myusername”和“myusername”。
I added the stripslashes() function when neither worked to test that, and still no dice.
我添加了 stripslashes() 函数,当两者都无法测试时,仍然没有骰子。
the error I get every time is: Warning: ldap_bind(): Unable to bind to server: Invalid credentials
我每次得到的错误是:警告:ldap_bind():无法绑定到服务器:凭据无效
any help would be greatly appreciated
任何帮助将不胜感激
TIA
TIA
回答by BentCoder
I know it is a pretty old question and if you still need an answer then what happens if you run this code in a single php file?
我知道这是一个很老的问题,如果您仍然需要答案,那么如果您在单个 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($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_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($ldap_connection);
More info is here.
更多信息在这里。
回答by Marco Ferrara
I used these functions:
我使用了这些功能:
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;
}
an this is my config.inc.php:
这是我的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";
?>
回答by Darkhan ZD
Check whether your login and pass correct. And before the login add domain. See in example bottom (HQ\login):
检查您的登录和密码是否正确。并在登录前添加域。请参见示例底部(HQ\login):
<?php
$login = 'HQ\student';
$password = 'MYPASS';
$ldap_link = ldap_connect('pdc.bc) or die("Could not connect to LDAP server.");
$ldapbind = @ldap_bind($ldap_link, $login, $password) or die ("Error trying to bind: ".ldap_error($ldap_link));
?>