我正在尝试使用PHP和LDAP对Active Directory进行身份验证。不幸的是,当我试图通过表单发送用户名时,它失败了。即使我在认证函数中指定了$userName和$passWord,该函数仍然返回false。我做错了什么?
PHP:<?php
session_start();
function authenticate($userName,$passWord) {
//Active Directory Server
$ldap_host = "example.com";
//Active Directory DN
$ldap_dn = "DC=myCompany,DC=org";
//Connect to AD Server
$ldap = ldap_connect($ldap_host)
or die("Could not connect to LDAP server.");
utf8_decode($passWord);
//Specify Active Directory
ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
//Verify userName and passWord
if($bind = @ldap_bind($ldap, $userName, $passWord)) {
//if valid
$ldapResults = 'Bind Successful';
return true;
} else {
//invalid username or password
return false;
}
};
if(isset($_POST['userName'])) {
//authenticate via function
if(authenticate($_POST['userName'],$_POST['passWord']))
{
//successfully authenticated (scroll down)
header("Location: example.com");
$error = null;
} else {
//failed authentication
$error = 1;
}
}
?>
HTML: <form method="post" id="loginForm" action="login.php">
<ul>
<li>
<input type="text" id="userName" name="userName" />
</li>
<li>
<input type="password" id="passWord" name="passWord" />
</li>
<li>
<input type="submit" name="submit" value="Log In"></input>
</li>
</ul>
</form>
由于您似乎正在使用ActiveDirectory进行身份验证,您可能会使用userName@myDomain
作为用户名而不是myDomain''username
。在这个答案中找到了。
您还应该能够为ldap_bind
函数提供完整的DN。
因此,您要么必须将其键入userName
字段,要么使用与已知(或匿名)用户的第二次绑定来获取给定用户名的DN,以便将其用于最终绑定。
DN
和提供的密码检查凭据。