PHP LDAP连接微软活动目录进行身份验证

点击打开链接

如果使用活动目录(Active Directory)代替在数据库表中建立账号, 你可以使用原来Windows网络中的账号.

LDAP, 轻量级目录访问协议(Lightweight Directory Access Protocol), 是用来访问微软的活动目录等目录服务器(DS, Directory Server)的协议. PHP默认支持LDAP.

下面是使用LDAP进行用户身份验证的PHP程序. 在微软的活动目录中, 用户的唯一标识是"samaccountname", 有些DS是"uid". 方法是:

  1. 用有权限的账号的dn(形如 cn=user_name,ou=web,dc=ideawu,dc=com)连接LDAP Server.
  2. 根据登录用户的名字查询其dn.
  3. 用该dn连接LDAP Server. 如果连接上就是登录成功.

注意! 微软的活动目录服务器可以使用空账号连接成功(设置问题? 默认? 特例?)!

$userid = $_POST['userid'];
$user_password = $_POST['password'];

if($userid && $user_password){
// config
// $ldap_server = "ideawu.com";
// $ldap_admin = "user_name";
// $ldap_password = "xxx";
// $base_cn = "ou=web,dc=ideawu,dc=com";
	$conn = ldap_connect($ldap_server);
	if(!$conn){
		die("<br>Connection LDAP server error");
	}
	$bind = ldap_bind($conn, $ldap_admin, $ldap_password);
	if(!$bind){
		die("<br>Bind LDAP server error");
	}

	$filter = 'samaccountname=' . $userid;
	$attributes = array('mail');	
	$result = ldap_search($conn, $base_dn, $filter, $attributes);
	$info = ldap_get_entries($conn, $result);

	if(!$result){
		die("<br>Search failed");
	}

	if($info["count"] != 0){
		$user_dn = $info[0]["dn"];
		unset($bind2);
		$bind2 = @ldap_bind($conn, $user_dn, $user_password);
		if($bind2){
			// Login done. Set session
		}
	}

	ldap_close($conn);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值