首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >LDAP身份验证上的无效DN语法

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

Stack Overflow用户
提问于 2014-10-13 20:27:32
回答 1查看 18.9K关注 0票数 3

我知道这一点以前已经得到了某种程度的回答,但它一直无法帮助我(除非它有,但由于我有限的php知识,它没有帮助)。下面是我的代码:

代码语言:javascript
运行
复制
<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)之后,我实际上正在接收我想要的数据。

EN

回答 1

Stack Overflow用户

发布于 2014-10-14 16:55:51

绑定到LDAP-Server的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-属性中具有用户名的条目。
  3. 从返回的条目获取DN (如果没有或多个条目,则不存在合适的用户,因此我们可以跳过其余条目)
  4. 再次使用退出的DN和提供的密码绑定到ldap。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26348338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档