无法使 LDAP 连接正常工作 - 总是 "Unknown error"

Cannot get LDAP connection working - always "Unknown error"

我正在研究构建 Active Directory 搜索工具所需的内容,该工具最终将用于 C# ASP.NET 网络应用程序。

我对 AD 知之甚少(除非必要,我不想知道更多)所以我要求我们的技术人员在服务器上设置一个虚拟实例。他们已经完成了给它的域名 dummy.local

我现在需要计算出我的 LDAP 连接字符串。在这里,我完全被困住了。我使用的用户帐户是 Domain Admins 的成员。在网上搜索了大量内容后,我尝试了各种方法来计算出 LDAP 连接字符串的各个组成部分。例如,如果我 运行 在 cmd.exe 中为服务器上的域管理员用户提供以下内容...

dsquery user -samid "username" | dsget user -memberof -expand

...这给了我以下信息:

"CN=Domain Admins,CN=Users,DC=dummy,DC=local"
"CN=Remote Desktop Users,CN=Builtin,DC=dummy,DC=local"
"CN=Users,CN=Builtin,DC=dummy,DC=local"
"CN=Administrators,CN=Builtin,DC=dummy,DC=local"
"CN=Domain Users,CN=Users,DC=dummy,DC=local"
"CN=Denied RODC Password Replication Group,CN=Users,DC=dummy,DC=local"

我还在 C# 控制台应用程序中 运行 以下...

using (var context = new PrincipalContext(ContextType.Domain))
using (var comp = ComputerPrincipal.FindByIdentity(context, Environment.MachineName))
{
    Console.WriteLine(String.Join(",", comp.DistinguishedName.Split(',').SkipWhile(s => !s.StartsWith("OU=")).ToArray()));
}

...这给了我以下信息:

OU=Domain Controllers,DC=dummy,DC=local

我想我因此拥有了构建我的 LDAP 连接字符串所需的所有属性。我最终得到了这个:

LDAP://COMSECWEBDEV.dummy.local/ou=Domain Controllers,/cn=Domain Admins,/cn=Users,/dc=dummy,/dc=local

我试过使用这个连接字符串,我知道域管理员的用户名和密码是正确的,但我尝试的一切总是给我同样的错误:

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
  at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
  at System.DirectoryServices.DirectoryEntry.Bind()
  at System.DirectoryServices.DirectoryEntry.get_AdsObject()
  at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
  at System.DirectoryServices.DirectorySearcher.FindOne()

因为这个错误没有给我任何细节,所以我不知道我做错了什么。我确定我只是没有得到正确的连接字符串,但我不知道如何计算出正确的字符串。

为了完整性,这里是我正在测试的控制台代码:

static void Main(string[] args)
{
    var connString = ConfigurationSettings.AppSettings["lc"];
    var username = ConfigurationSettings.AppSettings["lu"];
    var password = ConfigurationSettings.AppSettings["lpw"];

    using (DirectoryEntry de = new DirectoryEntry(connString, username, password))
    {
        DirectorySearcher search = new DirectorySearcher(de);

        search.PageSize = 1001;// To Pull up more than 100 records.

        DirectorySearcher directorySearcher = new DirectorySearcher(de);
        directorySearcher.Filter = string.Format("(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", username);

        directorySearcher.PropertiesToLoad.Add("msRTCSIP-PrimaryUserAddress");

        try
        {
            var result = directorySearcher.FindOne();

            var found = false;
            if (result != null)
            {
                if (result.Properties["msRTCSIP-PrimaryUserAddress"] != null)
                {
                    found = true;
                    Console.WriteLine("Found: " + result.Properties["msRTCSIP-PrimaryUserAddress"][0]);
                }
            }

            if (!found)
            {
                Console.WriteLine("Sad face");
            }
        }
        catch (Exception x)
        {
            Console.WriteLine(x.Message);
        }

        Console.WriteLine("------------");
    }
}

上周我试图弄清楚如何正确格式化 LDAP 连接字符串,并在 serverfault 上找到了这个条目:

How can I figure out my LDAP connection string?

我注意到您在每个 OU 或 DC 条目之间有一个“/”——我没有在我的条目中包含它们,而且我在上面的示例中也没有看到它们。

我远不是专家(显然),但我想我会把它扔在那里。