加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – LDAP问题,ldap_bind无效的dn语法

发布时间:2020-12-13 13:24:19 所属栏目:PHP教程 来源:网络整理
导读:我知道我的错误将会变得非常简单,但我试图找到问题并且我没有看到它,也许你可以帮助我…. 我试图用PHP创建一个函数,所以我可以连接到LDAP并找到所需的信息. 我的php代码如下: $ldapconfig['host'] = "127.0.0.1";$ldapconfig['port'] = NULL;$ldapconfig['b
我知道我的错误将会变得非常简单,但我试图找到问题并且我没有看到它,也许你可以帮助我….

我试图用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['bindpw']);
如错误中所述,您的绑定DN格式错误. DN表示对象的完整路径 – 所以在你的情况下应该是这样的(看起来你在AD?)

“cn = username,ou = domain users,dc = example,dc = com”

根据您的LDAP(Active Directory,OpenLDAP等)的风格,您可能可以使用uid(所以只是’用户名’)进行绑定,但最好假设您始终需要完整的DN.

您可以使用像Apache Directory Studio这样的LDAP工具来帮助构建查询并找出对象的DN.或者也有ldp.exe(如果是AD),但目录工作室更容易使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读