【问题标题】:php ldap_search in Active Directory root DN - empty resultsActive Directory 根 DN 中的 php ldap_search - 空结果
【发布时间】:2014-04-29 19:52:11
【问题描述】:

我正在尝试查询给定用户组成员的 Active Directory。 当我搜索特定用户的 OU 时,搜索正常。 当我尝试搜索整个目录时,结果为空。 由于我有很多用户 OU,特定用户 OU 可能会有所不同。

系统为CentOS 6.4,PHP+Apache。

代码如下:

$ldap_dn = "dc=ccc,dc=bbb,dc=aaa,dc=com";

// Active Directory user for querying
$query_user = "ldap_bind@ccc.bbb.aaa.com";
$password = "xxxx";

// Connect to AD
$ldap = ldap_connect($ldap_host, 389) or die("Could not connect to LDAP");
ldap_bind($ldap,$query_user,$password) or die("Could not bind to LDAP");
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

// Search AD
$results = ldap_search($ldap,$ldap_dn,"(sAMAccountName=$user)",array("memberof","primarygroupid"));
$entries = ldap_get_entries($ldap, $results);
if($entries['count'] == 0) {
    echo "No results\n";
    return false;
}

有人见过类似的结果吗?我是否缺少支持此类搜索的某些内容或任何配置?

【问题讨论】:

    标签: php linux ldap ldap-query


    【解决方案1】:

    正确的代码是...

    $ldap_dn = "dc=ccc,dc=bbb,dc=aaa,dc=com";
    
    // Active Directory user for querying
    $query_user = "ldap_bind@ccc.bbb.aaa.com";
    $password = "xxxx";
    
    // Connect to AD
    $ldap = ldap_connect($ldap_host, 389) or die("Could not connect to LDAP");
    
    ->>> bfore ldap_bind<<<- ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
    ->>> bfore ldap_bind<<<- ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
    
    ldap_bind($ldap,$query_user,$password) or die("Could not bind to LDAP");
    
    
    // Search AD
    $results = ldap_search($ldap,$ldap_dn,"(sAMAccountName=$user)",array("memberof","primarygroupid"));
    $entries = ldap_get_entries($ldap, $results);
    if($entries['count'] == 0) {
        echo "No results\n";
        return false;
    }
    

    【讨论】:

    • 关键是调用 ldap_set_option() ldap_connect() 和 ldap_bind() 之间。谢谢。
    猜你喜欢
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多