仅使用SamAccountName和Password从PHP使用LDAP验证用户


Authenticating user with LDAP from PHP with only SamAccountName and Password?

当我只有SamAccountName和Password时,我如何使用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"]; 

这对我有用。我花了好多天想弄明白这一点。

<?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>");   
} 
?>

实际上,答案是它取决于管理员如何配置LDAP服务器。您并不总是需要DN来对LDAP服务器进行身份验证。在我的特殊情况下,即使使用了DN,我仍然无法对LDAP服务器进行身份验证。对于我试图连接的LDAP服务器,它似乎是一个微软域,因此我只能在Domain中对user015使用Domain 'user015进行身份验证,其中user015是SamAccountName, Domain是该用户的域。但我可以鉴定。

感谢所有的帖子!即使他们不是正确的答案,他们确实帮助很大!

试试user@domain on dn…这对我很有效!

始终需要一个DN来对LDAP服务器进行身份验证。之后,您可以执行基于特定属性的过滤器,比如SamAccountName,但是您需要一个由DN标识的LDAP用户。

LDAP到AD的接口要求使用DN进行绑定。为了对用户进行身份验证,您必须首先找到该用户的DN——幸运的是,您可以通过搜索LDAP来找到该DN。

如果您将AD配置为允许匿名查询(除非您确定您可以降低安全性,否则不要这样做),您可以执行

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并继续重新绑定。