当我只有SamAccountName和密码时,如何使用LDAP从PHP进行身份验证?有没有办法绑定SamAccountName和密码,没有专有名称。我发现的唯一例子假设你有DN:
$server="XXX.XXX.XXX.XXX";
$dn = "cn=$username, ";
$basedn="ou=users, ou=accounts, dc=domain, dc=com";
if (!($connect = ldap_connect($server))) {
die ("Could not connect to LDAP server");
}
if (!($bind = ldap_bind($connect, "$dn" . "$basedn", $password))) {
die ("Could not bind to $dn");
}
$sr = ldap_search($connect, $basedn,"$filter");
$info = ldap_get_entries($connect, $sr);
$fullname=$info[0]["displayname"][0];
$fqdn=$info[0]["dn"];
答案 0 :(得分:15)
这对我有用。我花了很多天试图弄清楚这个。
<?php
//We just need six varaiables here
$baseDN = 'CN=Users,DC=domain,DC=local';
$adminDN = "YourAdminDN";//this is the admin distinguishedName
$adminPswd = "YourAdminPass";
$username = 'Username';//this is the user samaccountname
$userpass = 'UserPass';
$ldap_conn = ldap_connect('ldaps://yourADdomain.local');//I'm using LDAPS here
if (! $ldap_conn) {
echo ("<p style='color: red;'>Couldn't connect to LDAP service</p>");
}
else {
echo ("<p style='color: green;'>Connection to LDAP service successful!</p>");
}
//The first step is to bind the administrator so that we can search user info
$ldapBindAdmin = ldap_bind($ldap_conn, $adminDN, $adminPswd);
if ($ldapBindAdmin){
echo ("<p style='color: green;'>Admin binding and authentication successful!!!</p>");
$filter = '(sAMAccountName='.$username.')';
$attributes = array("name", "telephonenumber", "mail", "samaccountname");
$result = ldap_search($ldap_conn, $baseDN, $filter, $attributes);
$entries = ldap_get_entries($ldap_conn, $result);
$userDN = $entries[0]["name"][0];
echo ('<p style="color:green;">I have the user DN: '.$userDN.'</p>');
//Okay, we're in! But now we need bind the user now that we have the user's DN
$ldapBindUser = ldap_bind($ldap_conn, $userDN, $userpass);
if($ldapBindUser){
echo ("<p style='color: green;'>User binding and authentication successful!!!</p>");
ldap_unbind($ldap_conn); // Clean up after ourselves.
} else {
echo ("<p style='color: red;'>There was a problem binding the user to LDAP :(</p>");
}
} else {
echo ("<p style='color: red;'>There was a problem binding the admin to LDAP :(</p>");
}
?>
答案 1 :(得分:5)
实际上,答案是它取决于管理员如何配置LDAP服务器。您并不总是需要DN来对LDAP服务器进行身份验证。在我的特定情况下,即使使用DN,我仍然无法对LDAP服务器进行身份验证。对于我尝试连接的LDAP服务器,它似乎是一个Microsoft域,因此我只能使用DOMAIN \ user015对DOMAIN中的user015进行身份验证,其中user015是SamAccountName,DOMAIN是该用户的域。但我能够进行身份验证。
感谢所有帖子!即使他们不是正确的答案,他们确实帮了很多忙!
答案 2 :(得分:4)
在dn上试用user @ domain ...它对我有用!
答案 3 :(得分:0)
您始终需要DN才能对LDAP服务器进行身份验证。之后,您可以执行基于特定属性的过滤器,例如SamAccountName,但您需要由DN标识的LDAP用户。
答案 4 :(得分:0)
AD的LDAP接口要求您使用DN进行绑定。为了对用户进行身份验证,您必须首先找到该用户的DN - 幸运的是,您可以通过搜索LDAP找到DN。
如果您configure AD to allow anonymous queries(除非您确定安全性有所降低,否则不要这样做),您可以
ldap_bind($connect, "", "")
$sr = ldap_search($connect, $base_dn, "(sAMAccountName=$username)")
然后检索该用户的DN并继续使用用户的DN和密码重新绑定。
如果你不启用匿名绑定,那么你使用应用程序ID进行初始搜索,如下所示:
ldap_bind($connect, "DN=LDAP_App,OU=Users,DC=Domain,DC=com", "thePassword")
$sr = ldap_search($connect, $base_dn, "(sAMAccountName=$username)")
然后,如上所述,检索该用户的DN并继续重新绑定。