LDAP身份验证 - PHP(请帮助)搜索

时间:2013-08-19 08:55:16

标签: php login ldap intranet

好的,我已经改变了以前的代码。我还发现我没有开发版本工作! :/
我的新代码构造得更好,但我无法理解LDAP_SEARCH位,我得到的错误是:

错误:

Warning: ldap_search(): Search: Operations error in C:\inetpub\wwwroot\Intranet\login\index.php     on line 34
 Search on LDAP failed

我的代码:

<?php
// Application specific LDAP login
$app_user = 'cn=users,dc=DOMAIN, dc=local';
$app_pass = '';

// User-provided info (either from _POST or any way else)
// You should LDAP-escape $username here since it will be
//    used as a parameter for searches, but it's not a 
//    subject of this article. That one will follow soon. :-)
$username = 'USERNAME';
$password = PASSWORD;

// Here we'll put user's DN
$userdn = 'users';

// Connect to LDAP service
$conn_status = ldap_connect('SERVER.DOMAIN.local', 389);
if ($conn_status === FALSE) {
die("Couldn't connect to LDAP service");
 }

// Bind as application
$bind_status = ldap_bind($conn_status, $app_user, $app_pass);
if ($bind_status === FALSE) {
die("Couldn't bind to LDAP as application user");
}

// Find the user's DN
// See the note above about the need to LDAP-escape $username!
$query = "(&(uid=" . $username . ")(objectClass=user))";
$search_base = "cn=users,dc=DOMAIN, dc=local";
$search_status = ldap_search(
$conn_status, $search_base, $query, array('dn')
);
if ($search_status === FALSE) {
die("Search on LDAP failed");
}

// Pull the search results
$result = ldap_get_entries($conn_status, $search_status);
if ($result === FALSE) {
die("Couldn't pull search results from LDAP");
}

if ((int) @$result['count'] > 0) {
// Definitely pulled something, we don't check here
//     for this example if it's more results than 1,
//     although you should.
$userdn = $result[0]['dn'];
}

if (trim((string) $userdn) == '') {
die("Empty DN. Something is wrong.");
}

// Authenticate with the newly found DN and user-provided password
$auth_status = ldap_bind($conn_status, $userdn, $password);
if ($auth_status === FALSE) {
die("Couldn't bind to LDAP as user!");
}

print "Authentication against LDAP succesful. Valid username and password provided.";
?>

背景资料:

服务器位于我们的域中,并且从网络内部连接,因为该服务是一个内部网,不会在外部暴露给互联网。

1 个答案:

答案 0 :(得分:0)

  • 此客户端连接的LDAP目录服务器已使用operation error响应搜索请求,dn是特定的LDAP结果代码。应查阅目录服务器日志以确定此特定服务器拒绝搜索请求的原因。
  • 搜索请求应始终包含大小限制和时间限制,以及是否取消引用别名的概念。有些API在超出大小限制或时间限制时会产生错误的习惯。应避免使用这些API,但如果PHP是其中之一,请明确指定参数并检查是否未生成错误且未正确报告。
  • 搜索请求列出dn作为要返回的属性。 *不是属性,它是正在进行搜索的对象的主键。如果LDAP客户端希望从搜索请求返回属性及其值,则必须单独列出属性,或+将返回所有用户属性,1.1将返回所有操作属性(每个属性具有与其关联的访问控制可能会限制可以检索属性的授权状态)。如果LDAP客户端希望不从搜索返回任何属性,则客户端应使用属性列表中的OID {{1}}。在这种情况下,服务器将仅返回与搜索参数匹配的条目的DN。

另见