使用PHP LDAP有时会失败AD身份验证

时间:2017-12-05 02:02:45

标签: php authentication active-directory ldap

我目前正在使用我公司的Intranet社区网站。这里的工作人员可以在发布任何内容之前使用他们的AD登录凭证在网站上发布一些新闻或通知。

问题是,登录功能有时会起作用,有时却不起作用。我确定我使用正确的密码登录。当它遇到错误时,它将显示无效的凭据。仅供参考,我正在使用Apache在PHP WIN Server 2003 R2中运行PHP 5.4.22。

以下是我的登录页面代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>Intranet | Sharepoint - Login</title>
  <!-- Bootstrap core CSS-->
  <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <!-- Custom fonts for this template-->
  <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  <!-- Custom styles for this template-->
  <link href="css/sb-admin.css" rel="stylesheet">
</head>

<body class="bg-dark">
  <div class="container">
    <div class="card card-login mx-auto mt-5">
      <div class="card-header">Login</div>
      <div class="card-body">


        <form action="ldap.php" id="login" method="post">
            <label for="username">Username:</label>
            <input class="form-control" id="username" name="username" type="text" placeholder= "eg: 031074" required>
            <label for="password">Password:</label>
            <input class="form-control" id="password" name="password" type="password" placeholder= "Your Windows Login Password" required>                    
            <br />
            <input class="btn btn-primary btn-block" type="submit" value="Login">
        </form>

        <div class="text-center">
          <br>
          <a class="d-block small" href="forgot-password.html">Forgot Password?</a>
        </div>
      </div>
    </div>
  </div>
  <!-- Bootstrap core JavaScript-->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/popper/popper.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
  <!-- Core plugin JavaScript-->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
</body>

</html>

这是我的身份验证页面:

<?php
// config
$ldapserver = '192.168.X.XXX';
$ldapuser      = 'thongguan\administrator';  
$ldappass     = 'XXXXXXX';
$ldaptree    = "OU=ThongGuanUser,DC=thongguan,DC=local";

// connect d
$ldapconn = ldap_connect($ldapserver) or die("Could not connect to LDAP server.");

if($ldapconn) {
// binding to ldap server
ldap_set_option( $ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3 );
ldap_set_option( $ldapconn, LDAP_OPT_REFERRALS, 0 );


$ldapbind = ldap_bind($ldapconn, $ldapuser, $ldappass) or die ("Error trying to bind: ".ldap_error($ldapconn));
// verify binding
if ($ldapbind) {
$result = ldap_search($ldapconn,$ldaptree, "(samaccountname=".$_POST["username"].")") or die ("Error in search query: ".ldap_error($ldapconn));
$data = ldap_get_entries($ldapconn, $result);

for ($i=0; $i<$data["count"]; $i++) {
        //echo "dn is: ". $data[$i]["dn"] ."<br />";
        $usern= $data[$i]["cn"][0];
        if(isset($data[$i]["samaccountname"][0])) {
           // echo "ID: ". $data[$i]["samaccountname"][0] ."<br /><br />";
        } else {
           // echo "ID: None<br /><br />";
        }
    }
}
}


$ldap_dn = "cn=".$usern.",OU=ThongGuanUser,DC=thongguan,DC=local";
$ldap_password = $_POST["password"];

$ldap_con = ldap_connect("ldap://ADServer.thongguan.local");
ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);

if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password)){
    session_start();
    $_SESSION["authenticated"] = 'true';
    header('Location: adminpanel.php');
    $userlog= $_POST["username"];
    $_SESSION["userlog"] = $userlog;
}
else{
    header('Location: login.php');
}?>

问题是有时它可以验证用户,有时不会。但是在我的本地桌面PC上运行相同的代码就像魅力一样。

希望有人可以指出我的错误。

0 个答案:

没有答案