我正在尝试使用php ldap使用Windows Active Directory来认证Ldap。下面是代码:
$ldaphost = "abc.flyway.org";
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
if ($ds)
{
$username = "human";
$upasswd = "cryoutloud";
$ldapbind = ldap_bind($ds, $username, $upasswd);
if ($ldapbind)
{print "Congratulations! $username is authenticated.";}
else
{print "Nice try, Better luck next time!";}
}
当我将用户名留空时,我会收到祝贺消息,但是在使用用户名时,我收到错误。请协助
在$username = "human ";
行上,单词 human
之后应该有一个空格吗?对我来说看起来像一个错字。当您将其留空时,它只会尝试匿名绑定。尝试删除尾随空格。
您需要使用所谓的"可分辨名称"(DN)绑定到LDAP服务器。如果省略该字段并将"BindDN"参数留空,您将获得一个匿名登录,该登录可能会(也可能不会)起作用,具体取决于您正在与之通信的LDAP服务器。2013 f.i. 之后的 ActiveDirectory 不再允许匿名绑定。
DN 可能看起来有点像uid=<username>,dc=users,o=example,c=com
或完全不同的,但它绝不只是一个用户名。因此,如果要通过用户名对用户进行身份验证,则必须使用已知用户(您可以从LDAP-Admin获得)或匿名访问来搜索具有该用户名的用户的DN。
我已经创建了一个要点来说明这一点 https://gist.github.com/heiglandreas/5689592 - 如果您有更多关于 LDAP 的问题,请随时 ping 我。