在不知道Distinguished Name的情况下,使用ldap搜索或读取从AD检索属性


retrieving attributes from AD with ldap search or read without knowing the Distingushed Name

我试图在不知道所有使用域凭据登录我的应用程序的用户的DN的情况下从我的active directory中检索信息(描述和邮件),只有当我提供一个DN时,我才能检索信息,因为部门和组织单位的原因,每个DN对所有用户都非常不同,绑定工作正常,问题只是检索上面指定的数据。

我的代码如下。。注意:我更改了一些字段的值。

<?php
 ldap_authenticate();
 function ldap_authenticate() {
 //using ldap bind
 $ldaprdn  = 'username';//ldap rdn or dn
 $ldappass = 'password';// associated password
 $filter="(&(objectClass=user))";
 $justthese = array("sn","displayName");
 //connect to ldap server
 $ldapconn = ldap_connect("hostname.net");
 if ($ldapconn){
  //binding to ldap server
    $ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);
  //verify binding
    if($ldapbind){
        $dn='CN=Donald Mailula,OU=Users,OU=Group Testing,OU=Central Office,DC=hostname,DC=net';
        $sr=ldap_read($ldapconn,$dn,$filter);
        $entry = ldap_get_entries($ldapconn, $sr);
        echo $entry[0]["mail"][0] . " is the email address of the cn your requested<br/>";
        echo $entry[0]["description"][0];
    }else{
       echo "LDAP bind failed...";
    }
  }
}
?>

我的主要问题是,我无法知道所有登录用户的dn,所以我需要一种在没有dn的情况下搜索记录的方法,或者如何首先获得dn,然后搜索我需要的记录。

这可能吗,伙计们?

请帮助

谢谢D

您需要的东西叫做ldap_search,您可以在这里找到完整的样本

<?php
// $ds is a valid connexion id (samAccountName)
$dn = "o=Ma Compagnie, c=FR";
$filter="(|(objectCategory=person)(samAccountName=$ds))";
$justtheseattributes = array( "ou", "sn", "givenname", "mail");
$sr=ldap_search($ds, $dn, $filter, $justtheseattributes);
$info = ldap_get_entries($ds, $sr);
echo $info["count"]." found entries.'n";
?>