【问题标题】:Authenticating user using LDAP from PHP从 PHP 使用 LDAP 对用户进行身份验证
【发布时间】:2010-10-07 11:34:02
【问题描述】:

我的项目是为我们大学制作一个模块招生系统。因此,我联系了我大学的 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 

【问题讨论】:

  • 如果你的服务器是一个 linux 机器,如果没有适当的服务器证书,你将无法与 AD 对话。如果您需要大学的帮助,这可能是个问题。

标签: php authentication ldap


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

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

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

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

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

    这将使用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);
    

    【讨论】:

    • 只是按照你的一些步骤,让它像魅力一样工作......通过原生 PHP LDAP 模块路由。
    • 对使用此方法的人请注意:如果您的 ldap 服务器允许匿名登录,请确保在尝试身份验证之前将空密码过滤为无效。
    • 我正在针对 Active Directory 服务器进行身份验证,但仍然必须完成所有步骤到 7。
    【解决方案3】:

    你可以使用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());
    }
    

    【讨论】:

      【解决方案4】:

      @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. 
        }
        

      【讨论】:

      • 小心:当密码为空时,LDAP 的某些实现在 ldap_bind 上返回 true。您可能应该检查密码是否为空。
      猜你喜欢
      • 2011-07-23
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多