使用LDAP从PHP的用户进行身份验证 [英] Authenticating user using LDAP from PHP

查看:158
本文介绍了使用LDAP从PHP的用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是做一个模块招生制度对我们的大学。于是我联系了IT人在我大学的详细信息,以学生身份进入系统。我们利用现有的大学登录开发该系统。他们给了我一些LDAP信息,我不知道该使用。
我使用的是Apacha服务器上的PHP和MySQL。
我如何可以验证用户登录到我的系统,因为他与LDAP信息的用户名和密码。

下面给出

是LDAP信息(我已经改变了域名等)

有关blueroom.ac.uk域LDAP信息


  LDAP主机:ad.blueroom.ac.ukLDAP端口号:389BASE DN:OU =蓝鸟,直流=蓝鸟,DC = AC,DC =我的LDAP帐号绑定:CN = kikdap,OU = servacc,DC =蓝鸟,DC = AC,DC = UKLDAP帐号密码:********属性:sAMAccountName赋


解决方案

一般的程序是(有关转/括号内的LDAP PHP命令):


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


  2. 绑定使用LDAP帐户绑定LDAP服务器和LDAP帐户密码(的的ldap_bind()) - 如果你对身份验证Active Directory服务器就可以直接使用从登录页面输入用户名和密码,跳过以下所有步骤


  3. 搜索树由specifing基地DN和相应的LDAP过滤器的匹配用户条目/对象 - 最有可能类似(及(objectClass的=用户)(sAMAccountName赋= %S)),其中%S 应该由用户名代替,以进行身份​​验证(的 ldap_search()


  4. 检查,如果返回的条目数为1(如果<> 1,则出了问题,例如没有用户发现或多个用户找到)


  5. retrive的专有名称此单个条目(DN)( ldap_get_dn( )


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


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


这真的并不难,因为它的声音在第一。一般来说,我会建议使用某种标准库的反对LDAP服务器进行身份验证,如 Net_LDAP2 PEAR包或 Zend_Ldap Zend框架的。我有实际使用的经验 Net_LDAP2 (虽然我知道code相当不错),但 Zend_Ldap 作品非常对好Active Directory服务器或ADAMS服务器(这显然是您正在使用的)。

这会使用这样的伎俩 Zend_Ldap

  $选项=数组(
    '主机'= GT; ad.blueroom.ac.uk',
    useStartTls'=>真正,
    accountDomainName'=> blueroom.ac.uk',
    '的accountCanonicalForm'=> 4,
    '的baseDn'=> OU =蓝鸟,直流=蓝鸟,DC = AC,DC =我的',
);
$ LDAP =新Zend_Ldap($选项);
尝试{
    $ LDAP->绑定('用户','密码');
}赶上(Zend_Ldap_Exception $ E){
    //一些失败 - 检查$Ë
}
//绑定成功
$ acctname = $ LDAP的> getCanonicalAccountName(用户,Zend_Ldap :: ACCTNAME_FORM_DN);

My project is to make a module enrollment system for our university. So I contacted the IT people in my university for details to authenticate the students into the system. We are developing the system using the existing university login. They gave me some LDAP information, I don't know the usage of that. I'm using PHP,Mysql on an Apacha server. How can I authenticate a user logging into my system, given his userid and password with the LDAP information.

Given below is the LDAP information(i have changed the domain name etc.)

LDAP information for blueroom.ac.uk domain


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 

解决方案

The general procedure would be (relevant ext/ldap php commands in brackets):

  1. connect to LDAP server using the "LDAP Host" and "LDAP port no" (ldap_connect()) and set the correct connection options (ldap_set_option()), especially LDAP_OPT_PROTOCOL_VERSION and LDAP_OPT_REFERRALS

  2. bind to LDAP server using the "LDAP account to bind" and "LDAP account password" (ldap_bind()) - if you're authenticating against an Active Directory server you can directly use the username and password from the login page and skip all the following steps.

  3. search the tree for a matching user entry/object by specifing the "BASE DN" and the appropriate LDAP filter - most likely something like (&(objectClass=user)(sAMAccountName=%s)) where %s should be replaced by the username to be authenticated (ldap_search())

  4. check if the number of returned entries is 1 (if <> 1 then something has gone wrong, e.g. no user found or multiple users found)

  5. retrive the distinguished name (DN) of this single entry (ldap_get_dn())

  6. use the DN found in the last step to try to bind to the LDAP server with the password given at the authentication page (ldap_bind())

  7. if the bind succeeds then everything is OK, if not, most likely the password is wrong

It's really not as hard as it sounds at first. Generally I'd propose to use some sort of standard library for authenticating against a LDAP server such as the Net_LDAP2 PEAR package or Zend_Ldap out of the Zend Framework. I have no experience with actually using Net_LDAP2 (although I know the code quite well) but Zend_Ldap works very well against Active Directory servers or ADAMS servers (which is obviously what you're working with).

This will do the trick using 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);

这篇关于使用LDAP从PHP的用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆