簡體   English   中英

使用Active Directory對Intranet站點上的用戶進行身份驗證

[英]Using active directory to authenticate users on intranet site

我有一個我建立的'內部網'網站,它有自己的登錄系統(用戶注冊為新用戶,並使用其上的用戶名/密碼登錄該網站)。 但是,現在我想擴展它,讓Intranet站點使用現有的ActiveDirectory進行身份驗證。 這就是我正在尋找的,未來 -

當用戶訪問此Intranet站點( http://intranetsite/mySite )時,將根據活動目錄驗證用戶的域憑據,如果用戶的憑據與AD匹配,則會向用戶顯示Intranet站點的主頁。

我是AD的新手,不知道如何進行此配置。 我的Intranet站點是圍繞PHP構建的,並在應用程序服務器上使用Apache; AD位於不同的IIS服務器上。

我需要哪些信息,以及我將這些信息放在哪里(進入我的網站?htaccess?其他地方?)以便我可以使用AD身份驗證? 只是'配置'足夠,還是我需要為此身份驗證編寫顯式PHP代碼?

任何指針都非常感謝。

如果您只查看身份驗證而不是其他任何內容,那么您可能只需要幾行代碼即可。

首先,確保在php中啟用ldap

這是純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, 'username@domain.com', '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('username@domain.com', '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工作 - 它會為您節省大量時間,並且當事情無法正常工作時可能會感到沮喪。

此外,如果有疑問,您可以/應該使用哪些信息進行連接和驗證,請隨時查看我之前關於此主題的答案

這是我使用的:

<?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>

暫無
暫無

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

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