36

我的项目是为我们大学做一个模块招生系统。因此,我联系了我大学的 IT 人员以获取详细信息,以将学生身份验证到系统中。我们正在使用现有的大学登录开发系统。他们给了我一些 LDAP 信息,我不知道它的用途。我在 Apacha 服务器上使用 PHP、Mysql。给定用户 ID 和密码以及 LDAP 信息,我如何验证登录系统的用户。

下面给出的是 LDAP 信息(我已经更改了域名等)

blueroom.ac.uk 域的 LDAP 信息


LDAP Host : ad.blueroom.ac.uk

LDAP port no: 389

BASE DN : ou=bluebird, dc=bluebird, dc=ac, dc=my

LDAP account to bind : cn = kikdap, ou=servacc, dc=bluebird,dc=ac,dc=uk

LDAP account password : ********

Attribute : sAMAccountName 
4

4 回答 4

52

一般程序是(括号中的相关 ext/ldap php 命令):

  1. 使用“LDAP 主机”和“LDAP 端口号”(ldap_connect())连接到 LDAP 服务器并设置正确的连接选项(ldap_set_option()),尤其是LDAP_OPT_PROTOCOL_VERSIONLDAP_OPT_REFERRALS

  2. 使用“要绑定的 LDAP 帐户”和“LDAP 帐户密码”(ldap_bind())绑定到 LDAP 服务器 - 如果您要针对 Active Directory 服务器进行身份验证,则可以直接使用登录页面中的用户名和密码并跳过所有以下步骤。

  3. 通过指定“BASE DN”和适当的 LDAP 过滤器在树中搜索匹配的用户条目/对象 - 很可能类似于(&(objectClass=user)(sAMAccountName=%s))where%s应替换为要验证的用户名(ldap_search()

  4. 检查返回的条目数是否为 1(如果 <> 1 则表示出现问题,例如未找到用户或找到多个用户)

  5. 检索此单个条目的可分辨名称 (DN) ( ldap_get_dn() )

  6. 使用在上一步中找到的 DN 尝试使用身份验证页面 ( ldap_bind() )中提供的密码绑定到 LDAP 服务器

  7. 如果绑定成功,那么一切正常,如果没有,很可能是密码错误

这真的不像一开始听起来那么难。一般来说,我建议使用某种标准库来针对 LDAP 服务器(例如Net_LDAP2PEAR 包或Zend FrameworkZend_Ldap之外的服务器)进行身份验证。我没有实际使用的经验(尽管我非常了解代码),但对 Active Directory 服务器或 ADAMS 服务器(这显然是您正在使用的服务器)非常有效。Net_LDAP2Zend_Ldap

这将使用Zend_Ldap

$options = array(
    'host'                 => 'ad.blueroom.ac.uk',
    'useStartTls'          => true,
    'accountDomainName'    => 'blueroom.ac.uk',
    'accountCanonicalForm' => 4,
    'baseDn'               => 'ou=bluebird,dc=bluebird,dc=ac,dc=my',
);
$ldap = new Zend_Ldap($options);
try {
    $ldap->bind('user', 'password');
} catch (Zend_Ldap_Exception $e) {
    // something failed - inspect $e
}
// bind successful
$acctname = $ldap->getCanonicalAccountName('user', Zend_Ldap::ACCTNAME_FORM_DN);
于 2009-02-13T18:05:49.873 回答
4

您可以尝试http://code.activestate.com/recipes/101525/,同时参考http://us3.php.net/ldap和 Google 搜索[php ldap authentication]的其他结果。

于 2009-02-13T15:54:13.827 回答
3

@Stephen 提供了很好的观点。这是我使用 AD 进行身份验证的普通 PHP 代码:

  1. 首先你需要知道这个参数:服务器主机,用户域(如果你想查询AD,你还需要base dn)。
  2. 使用以下代码:

    $ldap = ldap_connect($host); // e.g. 165.5.54.6 or an URL
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); // Recommended for AD
    ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
    $bind = ldap_bind($ldap, $username.'@'.$userDomain, $passwrod);
    
    if($bind){
    // successful authentication. 
    }
    
于 2015-06-04T05:59:23.543 回答
2

你可以使用http://pear.php.net/package/Net_LDAP2/docs 它很好并且有效。

文档采用的连接示例:

// Inclusion of the Net_LDAP2 package:
require_once 'Net/LDAP.php';

// The configuration array:
$config = array (
    'binddn'    => 'cn=admin,ou=users,dc=example,dc=org',
    'bindpw'    => 'password',
    'basedn'    => 'dc=example,dc=org',
    'host'      => 'ldap.example.org'
);

// Connecting using the configuration:
$ldap = Net_LDAP2::connect($config);

// Testing for connection error
if (PEAR::isError($ldap)) {
    die('Could not connect to LDAP-server: '.$ldap->getMessage());
}
于 2009-07-23T19:25:38.600 回答