2013-07-21 55 views
25

我有一個我創建的「內部網」網站,它具有自己的登錄系統(用戶註冊爲新用戶,並使用用戶名/密碼登錄到現場)。但是,現在我想擴展它,並讓Intranet站點使用現有的ActiveDirectory進行身份驗證。這是我正在尋找,向前 -使用Active Directory來驗證內部網站上的用戶

當用戶訪問此內部網站點(http://intranetsite/mySite)時,用戶的域憑據根據活動目錄進行驗證,並且如果用戶的憑據匹配AD,則會呈現該用戶內部網站的主頁。

我是AD新手,不知道如何去做這個配置。我的內部網站是圍繞PHP構建的,並在應用程序服務器上使用Apache; AD位於不同的IIS服務器上。

我需要什麼信息,以及我在哪裏可以將這些信息(到我的網站?htaccess?其他地方?),以便我可以使用AD身份驗證?僅僅'配置'就夠了,還是我需要爲這個認證書寫明確的PHP代碼?

任何指針非常感謝。

+0

[adLDAP(http://adldap.sourceforge.net)庫可能對這個非常有幫助。 –

+0

您將要使用PHP的PHP庫,並向您的AD管理員請求具有「只讀」訪問權限的帳戶。 – DevlshOne

+0

讓我知道你什麼時候走得更遠,我會很高興地告訴你我設置了什麼。 – DevlshOne

回答

19

如果您只是在尋找身份驗證而沒有其他任何東西,那麼您只需要幾行代碼即可脫身。

首先,確保你的php中有ldap enabled

這裏是純PHP實現:
(請注意,這樣做時,這種方式,你應該確保你有一個用戶名和用戶密碼 - 匿名綁定幾乎總是對AD返回true)

$link = ldap_connect('domain.com'); // Your domain or domain server 

if(! $link) { 
    // Could not connect to server - handle error appropriately 
} 

ldap_set_option($link, LDAP_OPT_PROTOCOL_VERSION, 3); // Recommended for AD 

// Now try to authenticate with credentials provided by user 
if (! ldap_bind($link, '[email protected]', 'SomeSecret')) { 
    // Invalid credentials! Handle error appropriately 
} 
// Bind was successful - continue 

如果您希望使用Active Directory做更多有趣的事情,比如提取有關當前登錄用戶的一些信息,我強烈建議使用框架爲您完成繁重工作。如前所述,adLDAP是一個不錯的選擇,如果你運行PHP 5.4,我敢推薦我積極開發的AD-X庫(你可以通過Composer安裝它)。

與AD-X庫,你可以使用此代碼驗證用戶的憑據:

try { 
    $link = new ADX\Core\Link('domain.com'); // Establish connection to AD 
    $link->bind('[email protected]', 'SomeSecret'); // Authenticate user 
} 
catch (ADX\Core\ServerUnreachableException $e) { 
    // Unable to connect to server, handle error 
} 
catch (ADX\Core\InvalidCredentialsException $e) { 
    // Invalid credentials supplied 
} 
catch (Exception $e) { 
    // Something else happened, check the exception and handle appropriately 
} 

// Successfully authenticated if no exception has been thrown 

隨意選擇最適合您的。但是,如果您希望做的不僅僅是驗證,我強烈建議您爲ldap工作使用一個庫 - 這會爲您節省大量時間,並且可能會在您的工作無法正常工作時感到沮喪。

另外,如果有疑問,您可以/應該使用什麼信息連接和驗證,隨時檢查我的previous answer關於這個主題。

+0

儘管您確實演示瞭如何使用adLDAP連接並綁定到AD,但此代碼包含了如此之多的高級代碼,因此它有效地隱藏了提問者最感興趣的部分代碼。 – DevlshOne

+5

我的示例沒有演示任何adLDAP。你認爲哪一部分是先進的?你會如何推薦更新它?感謝您的解釋。 –

+2

我如何自動登錄用戶?你的方法需要用戶名和密碼。我如何獲得這些? – Somnium

6

下面是我用:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 'On'); 

define('DOMAIN_FQDN', 'mycompany.intra'); 
define('LDAP_SERVER', '192.168.0.1'); 

if (isset($_POST['submit'])) 
{ 
    $user = strip_tags($_POST['username']) .'@'. DOMAIN_FQDN; 
    $pass = stripslashes($_POST['password']); 

    $conn = ldap_connect("ldap://". LDAP_SERVER ."/"); 

    if (!$conn) 
     $err = 'Could not connect to LDAP server'; 

    else 
    { 
     define('LDAP_OPT_DIAGNOSTIC_MESSAGE', 0x0032); 

     ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3); 
     ldap_set_option($conn, LDAP_OPT_REFERRALS, 0); 

     $bind = @ldap_bind($conn, $user, $pass); 

     ldap_get_option($conn, LDAP_OPT_DIAGNOSTIC_MESSAGE, $extended_error); 

     if (!empty($extended_error)) 
     { 
      $errno = explode(',', $extended_error); 
      $errno = $errno[2]; 
      $errno = explode(' ', $errno); 
      $errno = $errno[2]; 
      $errno = intval($errno); 

      if ($errno == 532) 
       $err = 'Unable to login: Password expired'; 
     } 

     elseif ($bind) 
     { 
      $base_dn = array("CN=Users,DC=". join(',DC=', explode('.', DOMAIN_FQDN)), 
       "OU=Users,OU=People,DC=". join(',DC=', explode('.', DOMAIN_FQDN))); 

      $result = ldap_search(array($conn,$conn), $base_dn, "(cn=*)"); 

      if (!count($result)) 
       $err = 'Unable to login: '. ldap_error($conn); 

      else 
      { 
       foreach ($result as $res) 
       { 
        $info = ldap_get_entries($conn, $res); 

        for ($i = 0; $i < $info['count']; $i++) 
        { 
         if (isset($info[$i]['userprincipalname']) AND strtolower($info[$i]['userprincipalname'][0]) == strtolower($user)) 
         { 
          session_start(); 

          $username = explode('@', $user); 
          $_SESSION['foo'] = 'bar'; 

          // set session variables... 

          break; 
         } 
        } 
       } 
      } 
     } 
    } 

    // session OK, redirect to home page 
    if (isset($_SESSION['foo'])) 
    { 
     header('Location: /'); 
     exit(); 
    } 

    elseif (!isset($err)) $err = 'Unable to login: '. ldap_error($conn); 

    ldap_close($conn); 
} 
?> 
<!DOCTYPE html><head><title>Login</title></head> 
<style> 
* { font-family: Calibri, Tahoma, Arial, sans-serif; } 
.errmsg { color: red; } 
#loginbox { font-size: 12px; } 
</style> 
<body> 
<div align="center"><img id="imghdr" src="/img/logo.png" height="100" /><br><br><h2>Login</h2><br><br> 

<div style="margin:10px 0;"></div> 
<div title="Login" style="width:400px" id="loginbox"> 
    <div style="padding:10px 0 10px 60px"> 
    <form action="/login.php" id="login" method="post"> 
     <table><?php if (isset($err)) echo '<tr><td colspan="2" class="errmsg">'. $err .'</td></tr>'; ?> 
      <tr> 
       <td>Login:</td> 
       <td><input type="text" name="username" style="border: 1px solid #ccc;" autocomplete="off"/></td> 
      </tr> 
      <tr> 
       <td>Password:</td> 
       <td><input type="password" name="password" style="border: 1px solid #ccc;" autocomplete="off"/></td> 
      </tr> 
     </table> 
     <input class="button" type="submit" name="submit" value="Login" /> 
    </form> 
    </div> 
</div> 
</div> 
</body></html> 
相關問題