简体   繁体   English

为什么此LDAP查询返回的数组只有0

[英]Why does this LDAP query return an array with just a 0

I'm new to LDAP and Active Directory. 我是LDAP和Active Directory的新手。 I'm trying to fetch the Email-ID of an authenticated user using the following code. 我正在尝试使用以下代码来获取经过身份验证的用户的电子邮件ID。 However when I run it, all I get is an array with a 0 in it. 但是,当我运行它时,我得到的只是一个带有0的数组。

Here's the code 这是代码

$server ='ldaps://DOMAIN'; 
$username = 'DOMAIN\UID'; 
$password = 'PASSWORD';

$base_dn = 'dc=DOMAIN';
$search_filter = 'dn=UID'; 
$attributes = ['mail']; 

$ldap = ldap_connect($server);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_bind($ldap, $username, $password); 
$search = ldap_search($ldap, $base_dn, $search_filter, $attributes);
$data = ldap_get_entries($ldap, $search);

foreach($data as $dataPoint)
    {
        echo $dataPoint;
        echo "<hr>";
    }

This outputs just a 0 with a horizontal line below it. 这仅输出0,并且在其下方有一条水平线。

The most challenging thing here is that there is no error message whatsoever and I'm not very familiar with LDAP nor with Active Directory. 这里最具挑战性的是,没有任何错误消息,并且我对LDAP和Active Directory不太熟悉。

Any idea as to why this could be happening. 关于为什么会发生这种情况的任何想法。

I see several things that could potentially be causing problems from the code above: 我从上面的代码中看到了可能导致问题的几件事:

  • Do you really mean to use ldaps:// ? 您真的要使用ldaps://吗? Usually that is not what you want to do. 通常这不是您想要的。 If you want to use an encrypted connection you should use ldap_start_tls and the call should be made after ldap_connect . 如果要使用加密连接,则应使用ldap_start_tls,并且应在ldap_connect之后进行调用。 For the purpose of testing I would just change it to ldap:// . 为了进行测试,我将其更改为ldap://
  • Your $base_dn variable seems to be missing part of the domain. 您的$base_dn变量似乎缺少域的一部分。 That should not be the NETBIOS name of your domain, but rather the fully qualified domain name. 那不应该是您域的NETBIOS名称,而应该是完全合格的域名。 So if your domain was domain.com then the base dn would be dc=domain,dc=com . 因此,如果您的域是domain.com则基本dn将为dc=domain,dc=com
  • Your search filter ( $search_filter ) is not properly formed. 您的搜索过滤器( $search_filter$search_filter不正确。 If you are trying to retrieve a user object from LDAP given an account name, you could use a search filter like: (sAMAccountName=UID) 如果您尝试从给定帐户名的LDAP中检索用户对象,则可以使用类似以下的搜索过滤器: (sAMAccountName=UID)

To get a better idea of what may be going wrong you can use ldap_error and call it after you connect: echo "Error: ".ldap_error($ldap); 若要更好地了解可能出了什么问题,可以使用ldap_error并在连接后调用它: echo "Error: ".ldap_error($ldap); . You can do this after any LDAP related call to get more information on what may have gone wrong. 您可以在任何与LDAP相关的调用之后执行此操作,以获取有关可能出错的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM