PHP LDAP 訪問 Windows AD(Active Directory)

來源:互聯網
上載者:User



如果使用活動目錄(Active Directory)代替在資料庫表中建立帳號, 你可以使用原來Windows網路中的帳號.

LDAP, 輕量級目錄訪問協議(Lightweight Directory Access Protocol), 是用來訪問微軟的活動目錄等目錄伺服器(DS, Directory Server)的協議. PHP預設支援LDAP.

下面是使用LDAP進行使用者身分識別驗證的PHP程式. 在微軟的活動目錄中, 使用者的唯一標識是"samaccountname", 有些DS是"uid". 方法是:

   1. 用有許可權的帳號的dn(形如 cn=user_name,ou=web,dc=ideawu,dc=com)串連LDAP Server.
   2. 根據登入使用者的名字查詢其dn.
   3. 用該dn串連LDAP Server. 如果串連上就是登入成功.

注意! 微軟的活動目錄伺服器可以使用空帳號串連成功(設定問題? 預設? 特例?)!

$userid = $_POST[''userid''];
$user_password = $_POST[''password''];

if($userid && $user_password){
// config
// $ldap_server = "ideawu.com";
// $ldap_admin = "user_name";
// $ldap_password = "xxx";
// $base_cn = "ou=web,dc=ideawu,dc=com";
$conn = ldap_connect($ldap_server);
if(!$conn){
die("<br>Connection LDAP server error");
}
$bind = ldap_bind($conn, $ldap_admin, $ldap_password);
if(!$bind){
die("<br>Bind LDAP server error");
}

$filter = ''samaccountname='' . $userid;
$attributes = array(''mail'');
$result = ldap_search($conn, $base_dn, $filter, $attributes);
$info = ldap_get_entries($conn, $result);

if(!$result){
die("<br>Search failed");
}

if($info["count"] != 0){
$user_dn = $info[0]["dn"];
unset($bind2);
$bind2 = @ldap_bind($conn, $user_dn, $user_password);
if($bind2){
// Login done. Set session
}
}

ldap_close($conn);
}
 

</

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.