簡體   English   中英

LDAP認證-PHP(請幫助)搜索

[英]LDAP Authentication - PHP (Please Help) search

好的,我已經從以前的代碼中更改了代碼。 我還發現我沒有開發版本! :/
我的新代碼構造更好,但是我無法理解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.";
?>

背景信息:

該服務器位於我們的域中,並從網絡內部連接到該服務器,因為該服務是一個內部網,不會從外部暴露給Internet。

  • 該客戶端連接到的LDAP目錄服務器已對搜索請求作出了響應, operation error是特定的LDAP結果代碼。 應查閱目錄服務器日志以確定該特定服務器為何拒絕搜索請求。
  • 搜索請求應始終包括大小限制和時間限制,以及是否取消引用別名的概念。 當超出大小限制或時間限制時,某些API習慣於產生錯誤。 應避免使用這些API,但如果PHP是其中之一,則應明確指定參數並檢查是否未生成錯誤且未正確報告錯誤。
  • 搜索請求將dn列為要返回的屬性。 dn不是屬性,它是要對其進行搜索的對象的主鍵。 如果LDAP客戶端希望從搜索請求中返回屬性及其值,則必須單獨列出屬性,或者*將返回所有用戶屬性,而+將返回所有操作屬性(每個屬性都與之相關聯,訪問控制可能會限制可以檢索屬性的授權狀態)。 如果LDAP客戶端希望搜索不返回任何屬性,則客戶端應使用屬性列表中的OID 1.1 在這種情況下,服務器將僅返回與搜索參數匹配的條目的DN。

也可以看看

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM