LDAP身份验证上的DN语法无效


Invalid DN syntax on LDAP Authentication

我知道以前有人回答过这个问题,但它对我没有帮助(除非它有帮助,但由于我的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_min.php中的DN语法无效

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

绑定到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]))的筛选器来查找在mail、cn或uid属性中具有用户名的条目
  3. 从返回的条目中获取DN(如果没有或超过一个条目,则不存在合适的用户,因此我们可以跳过其余条目)
  4. 使用检索到的DN和提供的密码再次绑定到ldap

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