PHP ldap_bind()本地问题

问题描述 投票:0回答:1

我有一个脚本需要在我公司的Active Directory上显示用户列表。

为了执行它,我使用专用的AD帐户(有权在AD上搜索)。我这样做:

$ldapResource = ldap_connect('ldap://*********');
ldap_set_option($ldapResource, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapResource, LDAP_OPT_REFERRALS, 0);

ldap_bind($ldapResource, "CN=My company standard Account, OU=*******", "account_password");

然后,我正在进行搜索

ldap_search(...)

通过这样做,问题是当我在本地运行脚本(使用xampp)时,我失去了我当前的帐户权限(比如Internet访问,因为标准帐户没有这个权限?)。

如果我想以我的真实个人资料登录,我需要运行一个脚本,用我自己的凭据验证我(ldap_bind())。

有没有解决方案可以避免丢失我的主要身份验证?我应该以另一种方式进行研究吗?

php ldap
1个回答
0
投票

尝试下面的示例,看它是否有效。它列出了所有用户。根据您的需要更改变量。查看this了解更多示例。

$username = 'yowyow';
$password = '123123';
$server = '192.168.32.4';
$domain = '@yourdomain.local';
$port = 389;

$connection = ldap_connect($server, $port);
if (!$connection) {
    exit('Connection failed');
}

// Help talking to AD
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);

$bind = @ldap_bind($connection, $username.$domain, $password);
if (!$bind) {
    exit('Binding failed');
}

// This is where you can do your work
$unit = 'sales';
$distinguished_name = "OU=$unit,DC=yourdomain,DC=local";
$filter = "(sAMAccountName=*)";

$search = ldap_search($ldap_connection, $distinguished_name, $filter);
$total_record = ldap_count_entries($ldap_connection, $search);
$returned = ldap_get_entries($ldap_connection, $search);

if ($total_record > 0) {
    print_r($returned);
}
// End

ldap_close($ldap_connection);
© www.soinside.com 2019 - 2024. All rights reserved.