仅使用 SamAccountName 和密码通过 PHP 的 LDAP 对用户进行身份验证?
当我只有 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"];
how can I authenticate from PHP using LDAP when I only have the SamAccountName and Password? Is there a way to bind with just SamAccountName and Password and without Distinguished Name. The only examples I have found assume you have the 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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这对我有用。我花了很多天试图弄清楚这个问题。
This works for me. I spent many a days trying to figure this one out.
实际上,答案是这取决于管理员如何配置 LDAP 服务器。您并不总是需要 DN 来向 LDAP 服务器进行身份验证。在我的特定情况下,即使使用 DN,我仍然无法对 LDAP 服务器进行身份验证。对于我尝试连接的 LDAP 服务器,它似乎是一个 Microsoft 域,因此我只能使用 DOMAIN\user015 对 DOMAIN 中的 user015 进行身份验证,其中 user015 是 SamAccountName,而 DOMAIN 是该用户的域。但我能够验证。
感谢您的所有帖子!即使他们不是正确的答案,但他们确实有很大帮助!
Actually, the answer is that it depends on how the LDAP server was configured by the admin. You don't always need a DN to authenticate to an LDAP server. In my particular case, even with the DN, I still couldn't authenticate to the LDAP server. For the LDAP server I was trying to connect, it appears it was a Microsoft Domain, and so I could only authenticate with DOMAIN\user015 for user015 in DOMAIN where user015 is a SamAccountName and DOMAIN is the domain for that user. But I was able to authenticate.
Thank you for all the posts! Even if they weren't the correct answer, they did help a lot!
尝试 dn 上的 user@domain ...它对我有用!
Try user@domain on dn... It worked for me!
您始终需要 DN 来向 LDAP 服务器进行身份验证。之后,您可以根据特定属性(例如 SamAccountName)执行过滤器,但您需要由 DN 标识的 LDAP 用户。
You always need a DN to authenticate to a LDAP server. After that, you can perform a filter based in a specific attribute, like SamAccountName, but you need an LDAP user identified by a DN.
AD 的 LDAP 接口要求您使用 DN 进行绑定。为了对用户进行身份验证,您必须首先找到该用户的 DN — 幸运的是,您可以通过搜索 LDAP 找到 DN。
如果您将 AD 配置为允许匿名查询(除非您确定自己是好吧,安全性有所降低),您可以执行此操作
,然后检索该用户的 DN 并继续使用该用户的 DN 和密码重新绑定。
如果您未启用匿名绑定,则可以使用应用程序 ID 进行初始搜索,如下所示:
然后,如上所述,检索该用户的 DN 并继续重新绑定。
The LDAP interface to AD requires that you bind using a DN. In order to authenticate a user, you must first find that user's DN — fortunately, you can find the DN by searching LDAP.
If you configure AD to allow anonymous queries (don't do this unless you are sure you're ok with the reduction in security), you can do
And then retrieve that user's DN and proceed to rebind with the user's DN and password.
If you do not enable anonymous bind, then you use an application ID to do the initial search, like so:
And then, just as above, retrieve that user's DN and proceed to rebind.