使用 PHP 的 LDAP 身份验证
LDAP Authentication with PHP
如何使用 php
进行 LDAP 身份验证
我在 xampp 成功连接了 ldap 服务器。
LDAP 绑定成功
但处理此代码时出现一些错误
<?php
$ldapBaseDomain = 'ou=employee,dc=domainname';
$ldapServer = 'domainname';
$ldapUsername = 'xxx';
$ldapPassword = 'yyy';
if (!empty($_POST['username']) && !empty($_POST['password'])) {
if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_POST['username'])) {
die('Please enter a valid username');
}
if (!$ldapConnection = @ldap_connect($ldapServer)) {
die('Could not connect to ldap server');
}
if (!@ldap_bind($ldapConnection, $ldapUsername, $ldapPassword)) {
die('Could not bind to ldap server');
}
if (!$ldapSearch = @ldap_search($ldapConnection, $ldapBaseDomain, $_POST['username'])) {
die('Could not complete ldap search');
}
$ldapCount = @ldap_count_entries($ldapConnection, $ldapSearch);
if (!$ldapCount) {
die('account not found');
} else {
if (!$ldapEntry = @ldap_get_entries($ldapConnection, $ldapSearch)) {
die('Could not get ldap entry');
}
$distinguishedName = $ldapEntry[0]['distinguishedname'][0];
if (empty($distinguishedName)) {
die('Account information not found');
}
if(!@ldap_bind($ldapConnection, $distinguishedName, $_POST['password'])) {
die('Password Incorrect');
}
echo ' <h1>Logged in successfully</h1>
<h2>User Details</h2>';
echo '<pre>' . print_r($ldapEntry[0], true) . '</pre>';
}
} else {
echo ' <form method="post">
Username: <input name="username"><br>
Password: <input name="password" type="password"><br>
<input type="submit" value="login">
</form>';
}
?>
这是错误显示
Could not complete ldap search
解决这个问题的任何提示
在 LDAP 搜索中,您必须使用有效的 LDAP 过滤器。如果用户名是 'johndoe',您目前只是在搜索 'johndoe'。这就像执行以下 SQL-搜索:
SELECT * FROM users WHERE 'johndoe';
您必须让 LDAP 服务器有机会知道在何处搜索“'johndoe'”。因此,您应该使用 sAMAccountName=johndoe
(对于 ActiveDirectory)或 uid=johndoe
(对于 OpenLdap)之类的东西。
有关更深入的示例,请查看 https://gist.github.com/heiglandreas/5689592
如何使用 php
进行 LDAP 身份验证我在 xampp 成功连接了 ldap 服务器。
LDAP 绑定成功
但处理此代码时出现一些错误
<?php
$ldapBaseDomain = 'ou=employee,dc=domainname';
$ldapServer = 'domainname';
$ldapUsername = 'xxx';
$ldapPassword = 'yyy';
if (!empty($_POST['username']) && !empty($_POST['password'])) {
if (!preg_match('/^[a-zA-Z0-9\-]+$/', $_POST['username'])) {
die('Please enter a valid username');
}
if (!$ldapConnection = @ldap_connect($ldapServer)) {
die('Could not connect to ldap server');
}
if (!@ldap_bind($ldapConnection, $ldapUsername, $ldapPassword)) {
die('Could not bind to ldap server');
}
if (!$ldapSearch = @ldap_search($ldapConnection, $ldapBaseDomain, $_POST['username'])) {
die('Could not complete ldap search');
}
$ldapCount = @ldap_count_entries($ldapConnection, $ldapSearch);
if (!$ldapCount) {
die('account not found');
} else {
if (!$ldapEntry = @ldap_get_entries($ldapConnection, $ldapSearch)) {
die('Could not get ldap entry');
}
$distinguishedName = $ldapEntry[0]['distinguishedname'][0];
if (empty($distinguishedName)) {
die('Account information not found');
}
if(!@ldap_bind($ldapConnection, $distinguishedName, $_POST['password'])) {
die('Password Incorrect');
}
echo ' <h1>Logged in successfully</h1>
<h2>User Details</h2>';
echo '<pre>' . print_r($ldapEntry[0], true) . '</pre>';
}
} else {
echo ' <form method="post">
Username: <input name="username"><br>
Password: <input name="password" type="password"><br>
<input type="submit" value="login">
</form>';
}
?>
这是错误显示
Could not complete ldap search
解决这个问题的任何提示
在 LDAP 搜索中,您必须使用有效的 LDAP 过滤器。如果用户名是 'johndoe',您目前只是在搜索 'johndoe'。这就像执行以下 SQL-搜索:
SELECT * FROM users WHERE 'johndoe';
您必须让 LDAP 服务器有机会知道在何处搜索“'johndoe'”。因此,您应该使用 sAMAccountName=johndoe
(对于 ActiveDirectory)或 uid=johndoe
(对于 OpenLdap)之类的东西。
有关更深入的示例,请查看 https://gist.github.com/heiglandreas/5689592