【问题标题】:LDAP issue, ldap_bind invalid dn syntaxLDAP 问题,ldap_bind 无效的 dn 语法
【发布时间】:2012-11-21 05:55:27
【问题描述】:

我知道我的错误将是非常简单的,但我试图找到问题,但我没有看到它,也许你可以帮助我....

我正在尝试使用 php 创建一个函数,以便能够连接到 LDAP 并找到所需的信息。

我的php代码如下:

$ldapconfig['host'] = "127.0.0.1";
$ldapconfig['port'] = NULL;
$ldapconfig['basedn'] = "dc=example,dc=com";
$ldapconfig['binddn'] = "user";
$ldapconfig['bindpw'] = "password";


function ldap_authenticate($user, $pass) {
global $ldapconfig;
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); 
if ($user != "" && $pass != "") {
    $ds=ldap_connect($ldapconfig['host'],$ldapconfig['port']);
    if(!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
        return NULL;
    }
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
    ldap_bind( $ds, $ldapconfig['binddn'], $ldapconfig['bindpw']);
    $r = ldap_search( $ds, $ldapconfig['basedn'], 'sAMAccountName=' . $user);
    if ($r) {
        $result = ldap_get_entries( $ds, $r);
        if ($result[0]) {
            if (ldap_bind( $ds, $result[0]['dn'], $pass) ) {
                return $result[0]['mail'][0];
            }
        }
    }
}
return NULL;

当我尝试运行代码时,它给了我以下错误: ldap_bind 第 xxxx 行的 DN 语法无效 该行如下:

ldap_bind( $ds, $ldapconfig['binddn'], $ldapconfig['bindpw']);

【问题讨论】:

    标签: php ldap


    【解决方案1】:

    如错误中所述,您的绑定 DN 格式错误。 DN 代表对象的完整路径 - 所以在你的情况下应该是这样的(看起来你在 AD 上?)

    "cn=username,ou=域用户,dc=example,dc=com"

    根据您的 LDAP(Active Directory、OpenLDAP 等)的风格,您可能能够使用 uid(所以只是“用户名”)进行绑定,但最好假设您总是需要完整的 DN。

    您可以使用像 Apache Directory Studio 这样的 LDAP 工具来帮助构建查询并找出对象的 DN 是什么。或者也有ldp.exe(前提是AD),但是directory studio更容易使用。

    【讨论】:

    • 对于简单的认证方法,DN是必需的。对于某些 SASL 方法,需要用户名或专有名称。
    • 感谢答案,对我帮助很大。
    • 是否可以使用 custom(即我选择的)属性作为登录用户名?例如inetOrgPerson 对象类的 mail 属性? (使用OpenLDAP
    【解决方案2】:

    在 DC 上,执行: dsquery 用户 -samid jim

    将显示与 sAMAccountName 匹配的用户的 DN: "CN=Jim Willeke,CN=Users,DC=mad,DC=willeke,DC=com"

    http://ldapwiki.willeke.com/wiki/LDAP%20and%20Active%20Directory

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 2013-10-24
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      相关资源
      最近更新 更多