LDAP问题,ldap_bind无效的DN语法。

9

我知道我的错误可能是非常简单的,但我已经尝试找到问题并且没有看到它,也许你可以帮助我...

我正在尝试使用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无效的DN语法,位于xxxx行 而该行代码如下:
ldap_bind( $ds, $ldapconfig['binddn'], $ldapconfig['bindpw']);
2个回答

15
如错误所述,您的绑定DN格式错误。 DN表示对象的完整路径 - 因此在您的情况下应该像这样(看起来您正在使用AD?)
“cn = username,ou = domain users,dc = example,dc = com”
根据您使用的LDAP版本(Active Directory,OpenLDAP等),您可能能够使用uid(因此只需“用户名”)进行绑定,但最好假设您始终需要完整的DN。
您可以使用LDAP工具(例如Apache Directory Studio)来帮助构建查询并找出对象的DN是什么。或者还有ldp.exe(如果提供了AD),但目录工作室更容易使用。

1
对于简单认证方法,需要使用DN。对于某些SASL方法,需要使用用户名或可分辨名称。 - Terry Gardner
谢谢你的回答,它帮了我很多。 - Humberto
能否使用自定义属性(即由我选择)作为用户名进行登录?例如,inetOrgPerson对象类的mail属性?(使用OpenLDAP - pkaramol

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接