简体   繁体   English

使用ldap / php查询AD

[英]querying AD using ldap / php

Background Information 背景资料

I'm trying to figure out how to query our active directory server for information about users / groups via a php web application. 我试图弄清楚如何通过php Web应用程序查询活动目录服务器中有关用户/组的信息。 (let's call it the "widget app". Ultimately, I'm going to use this information to try to "see" what fields / data is available in AD to check / use as a part of authentication besides just username and password. For example, I only want to allow people in specific AD groups ... etc. (我们称它为“小工具应用程序”。最终,我将使用此信息来尝试“查看” AD中除了用户名和密码之外哪些字段/数据可用于检查/用作身份验证的一部分。对于例如,我只想允许特定AD组中的人...等等。

I'm using this as an example: http://php.net/manual/en/ldap.examples-basic.php 我以这个为例: http : //php.net/manual/en/ldap.examples-basic.php

Problem 问题

Unfortunately, I'm getting zero results... even when I use my AD username as the filter. 不幸的是,即使使用我的AD用户名作为过滤器,我的结果也为零。

this is what my results look like: 这就是我的结果:

Connecting ...connect result is Resource id #26
Binding ...Bind result is 1
Searching for (sn=myusername*) ...Search result is Resource id #27

Getting entries ...
Data for 0 items returned:

What I've tried so far: 到目前为止,我已经尝试过:

We have another web application that's running on the same web server as the widget app. 我们还有另一个与小部件应用程序在同一Web服务器上运行的Web应用程序。 This other application is set up so that apache will prompt for AD credentials. 设置了另一个应用程序,以便apache提示输入AD凭据。 I know it works because when I try to authenticate myself on this secondary application, my AD credentials are authenticated and i'm given the authorization I need to use the application. 我知道它有效,因为当我尝试在此辅助应用程序上对自己进行身份验证时,我的AD凭据已通过身份验证,并且获得了我使用该应用程序所需的授权。

So I started to poke around the apache conf and tried to make sure my PHP code is using the same values. 因此,我开始四处查看apache conf,并尝试确保我的PHP代码使用相同的值。

The Code 编码

Here's the PHP code that's currently failing: 这是当前失败的PHP代码:

public function ldap_test() {
            echo "<h3>LDAP query test</h3>";
            echo "Connecting ...";
            $ds=ldap_connect("10.11.11.1111");  // must be a valid LDAP server!
            echo "connect result is " . $ds . "<br />";

            if ($ds) {
                echo "Binding ...";
                //$r=ldap_bind($ds);
$r=ldap_bind($ds,"CN=testvalue1,OU=Services,OU=Accounts,DC=td,DC=ab,DC=org", "somepasswordvalue"); 
                // read-only access
                echo "Bind result is " . $r . "<br />";
                echo "Searching for (sn=myusername*) ...";

                // Search surname entry
                $sr=ldap_search($ds, "CN=testvalue1,OU=Services,OU=Accounts,DC=td,DC=ab,DC=org", "somepasswordvalue", "(sAMAccountName=myusername*)");
                echo "Search result is " . $sr . "<br />";

                echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";

                echo "Getting entries ...<p>";
                $info = ldap_get_entries($ds, $sr);
                echo "Data for " . $info["count"] . " items returned:<p>";

                for ($i=0; $i<$info["count"]; $i++) {
                    echo "dn is: " . $info[$i]["dn"] . "<br />";
                    echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
                    echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
                }
                echo "Closing connection";
                ldap_close($ds);
            } else {
                echo "<h4>Unable to connect to LDAP server</h4>";
            }
    }

Apache configuration that I used to build my PHP code: (this config works and properly prompts me for my AD credentials and authenticates properly) 我用来构建PHP代码的Apache配置:(此配置有效并正确提示我输入AD凭据并正确进行身份验证)

<AuthnProviderAlias ldap ldap-test>
    AuthLDAPBindDN "CN=testvalue1,OU=Services,OU=Accounts,DC=td,DC=ab,DC=org"
    AuthLDAPBindPassword somepasswordvalue
    AuthLDAPURL "ldap://10.11.11.111/ou=Accounts,dc=td,dc=ab,dc=org?sAMAccountName?sub?(objectClass=*)"
    AuthLDAPMaxSubGroupDepth 5
</AuthnProviderAlias>

This is the first time I've tried to do AD authentication in PHP and I'm not the one who manages our AD implementations so I'm fairly green. 这是我第一次尝试使用PHP进行AD身份验证,但我不是管理AD实现的人,因此我非常环保。 If you have any suggestions for me please feel free. 如果您对我有任何建议,请随时。

Thanks 谢谢

The problem was that I was filtering by a common name. 问题是我正在按通用名称过滤。 Notice this: 注意:

 AuthLDAPBindDN "CN=testvalue1,OU=Services,OU=Accounts,DC=td,DC=ab,DC=org"

So to fix it , i just had to remove this from the filter and it worked. 因此,要修复它,我只需要从过滤器中删除它即可。

 AuthLDAPBindDN "OU=Services,OU=Accounts,DC=td,DC=ab,DC=org"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM