【问题标题】:LDAP Authentication with PHP使用 PHP 进行 LDAP 身份验证
【发布时间】:2015-06-20 09:35:42
【问题描述】:

如何使用 php 进行 LDAP 认证

我在xampp中成功连接了ldap服务器。

LDAP 绑定成功

但在处理此代码时会显示一些错误

<?php


    $ldapBaseDomain = 'ou=employee,dc=domainname';
    $ldapServer = 'domainname';
    $ldapUsername = 'xxx';
    $ldapPassword = 'yyy';


if (!empty($_POST['username']) && !empty($_POST['password'])) {


    if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_POST['username'])) {
        die('Please enter a valid username');
    }

    if (!$ldapConnection = @ldap_connect($ldapServer)) {
        die('Could not connect to ldap server');
    }

    if (!@ldap_bind($ldapConnection, $ldapUsername, $ldapPassword)) {
        die('Could not bind to ldap server');
    }

    if (!$ldapSearch = @ldap_search($ldapConnection, $ldapBaseDomain, $_POST['username'])) {
        die('Could not complete ldap search');
    }

    $ldapCount = @ldap_count_entries($ldapConnection, $ldapSearch);

    if (!$ldapCount) {
        die('account not found');
    } else {
        if (!$ldapEntry = @ldap_get_entries($ldapConnection, $ldapSearch)) {
            die('Could not get ldap entry');
        }

        $distinguishedName = $ldapEntry[0]['distinguishedname'][0];

        if (empty($distinguishedName)) {
            die('Account information not found');
        }

        if(!@ldap_bind($ldapConnection, $distinguishedName, $_POST['password'])) {
            die('Password Incorrect');
        }

        echo '  <h1>Logged in successfully</h1>
                <h2>User Details</h2>';

        echo '<pre>' . print_r($ldapEntry[0], true) . '</pre>';
    }

} else {
    echo '  <form method="post">
            Username: <input name="username"><br>
            Password: <input name="password" type="password"><br>
            <input type="submit" value="login">
            </form>';
}

?>

这是错误提示

无法完成 ldap 搜索

解决这个问题的任何提示

【问题讨论】:

  • 你能用“tail -f /var/logs/dirsrv/access”把LDAP服务器的访问日志放上去吗
  • sry 我正在使用 windows AD 那么它怎么可能??

标签: php authentication ldap authorization


【解决方案1】:

在 LDAP 搜索中,您必须使用有效的 LDAP 过滤器。如果用户名是“johndoe”,您当前只是在搜索“johndoe”。这就像执行以下 SQL 搜索:

SELECT * FROM users WHERE 'johndoe';

您必须让 LDAP 服务器有机会知道在哪里搜索 '''johndoe'''。因此,您应该使用sAMAccountName=johndoe(用于 ActiveDirectory)或uid=johndoe(用于 OpenLdap)之类的东西。

有关更深入的示例,请查看https://gist.github.com/heiglandreas/5689592

【讨论】:

  • ldap_search 中删除@ 时是否收到任何错误消息?
  • 使用你的例子我应该声明 $host = 'ip'; $port = '端口';像这样
  • 那么确切的错误信息是什么?您可能还想致电ldap_errnoldap_error
  • 这是错误出现 --- 解析错误:语法错误,意外“无连接”(T_CONSTANT_ENCAPSED_STRING)
猜你喜欢
  • 2011-07-23
  • 2014-02-28
  • 2015-10-03
  • 2014-03-08
  • 2014-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多