【问题标题】:Invalid DN syntax on LDAP AuthenticationLDAP 身份验证上的 DN 语法无效
【发布时间】:2014-12-08 12:15:32
【问题描述】:

我知道这个问题之前已经得到解答,但它无法帮助我(除非它有,但由于我有限的 php 知识它没有帮助)。下面是我的代码:

<body>
<html>     

<?php
//echo var_dump($_POST);
        $user = "".$_POST["username"]."";
        settype($user, "string");
        $password = $_POST["password"];
        $ldap_host = "ldap.burnside.school.nz";
        $base_dn = "ou=students,o=bhs";
        $ldap_user = "(cn=".$user.")";
        $filter = "($ldap_user)"; // Just results for this user
        $ldap_pass = "".$password."";

        $connect = ldap_connect($ldap_host)
                or exit(">>Could not connect to LDAP server<<");
        ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);

        // This next bit is the important step.  Bind, or fail to bind.  This tests the username/password.        
        if (ldap_bind($connect, $ldap_user.",".$base_dn, $ldap_pass)) {
            $read = ldap_search($connect, $base_dn, $filter)
                or exit(">>Unable to search ldap server<<");

            // All the next 8 lines do is get the users first name.  Not required
            $info = ldap_get_entries($connect, $read);
            $ii = 0;
            for ($i = 0; $ii < $info[$i]["count"]; $ii++) {
                $data = $info[$i][$ii];
                if ($data == "givenname") {
                    $name = $info[$i][$data][0];
                }
            }

            ldap_close($connect);
            header("Location: success.php?name=$name");
        } 
        else {
            ldap_close($connect);
            //header("Location: failure.php?user=$user");
        }
        ?>

</body>
</html>

我在第 21 行遇到错误,当我绑定到服务器时说:

警告:ldap_bind():无法绑定到服务器:第 21 行的 S:\XAMPP\htdocs\PhpProject1\LDAP_main.php 中的 DN 语法无效

有人能解决这个问题吗?当我在代码中实现我的$_POST 以接收用户名和密码时,它才开始发生,但正如您在我注释掉的// echo var_dump($_POST) 中看到的那样,我实际上正在接收我想要的数据。

【问题讨论】:

    标签: php syntax ldap dn


    【解决方案1】:

    您用于绑定到 LDAP 服务器的 DN 是 (cn=[username]),ou=students,o=bhs,这不是有效的 DN 语法。那应该是cn=[username],ou=students,o=bhs,没有大括号。

    您将 LDAP 过滤器(大括号内的东西)与 DN 混淆了。

    我会通过以下方式进行 LDAP 身份验证:

    1. 匿名绑定或与您知道 DN 的默认用户绑定
    2. 使用该用户搜索与包含所提供用户名的特定过滤器匹配的所有用户。您可以使用(|(mail=[username])(cn=[username])(uid=[username])) 之类的过滤器来查找邮件中包含用户名、cn 或 uid-attribute 的条目
    3. 从返回的条目中获取 DN(如果没有或不止一个条目,则表示不存在合适的用户,因此我们可以跳过其余部分)
    4. 使用检索到的 DN 和提供的密码再次绑定到 ldap。

    看看https://gist.github.com/heiglandreas/5689592

    【讨论】:

    • 我已经这样做了,我收到错误搜索过滤器的错误?在我使用预先输入的用户名和密码之前,这一切都可以正常工作,但是由于我使用 post 方法获取数据,它开始无法正常工作,所以我认为问题与此有关?除非你知道如何解决新问题; “警告:ldap_search():搜索:第 22 行 S:\XAMPP\htdocs\PhpProject1\LDAP_main.php 中的错误搜索过滤器>>无法搜索 ldap 服务器
    • 您的搜索过滤器到底是什么?
    • 放一些调试语句并查看你构造的变量,这样你就知道你的代码在做什么。然后你至少会知道你提交给 LDAP 服务器的值是什么。并获得一个好的 LDAP 浏览器,它会向您显示需要的值。 (我使用 Apache Studio directory.apache.org/studio
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多