首页 文章

使用PHP从LDAP验证用户

提问于
浏览
35

我的项目是为我们的大学制作模块注册系统 . 所以我联系了我大学的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

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

    doc所做的连接示例:

    // 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());
    }
    
  • 52

    您可以在引用http://us3.php.net/ldap时尝试http://code.activestate.com/recipes/101525/,以及Google搜索[php ldap authentication]的其他结果 .

  • 2

    @Stephen提供了很好的分数 . 这是我使用AD进行身份验证的纯PHP代码:

    • 首先你需要知道这个参数:服务器主机,用户域(如果你想查询AD,你还需要基数dn) .

    • 使用以下代码:

    $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. 
    }
    
  • 2

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

    • 使用"LDAP Host"和"LDAP port no"(ldap_connect())连接到LDAP服务器并设置正确的连接选项(ldap_set_option()),尤其是 LDAP_OPT_PROTOCOL_VERSIONLDAP_OPT_REFERRALS

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

    • 通过指定"BASE DN"和相应的LDAP过滤器在树中搜索匹配的用户条目/对象 - 很可能是 (&(objectClass=user)(sAMAccountName=%s)) ,其中 %s 应替换为要进行身份验证的用户名(ldap_search()

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

    • 检索此单个条目的专有名称(DN)(ldap_get_dn()

    • 使用上一步中找到的DN尝试使用身份验证页面上给出的密码绑定到LDAP服务器(ldap_bind()

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

    它建议使用某种标准库来验证LDAP服务器,例如Net_LDAP2 PEAR包或Zend Framework中的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);
    

相关问题