ddap身份验证的语法错误

时间:2012-09-21 16:58:00

标签: .net authentication ldap dn

我在编写可以执行LDAP身份验证的模块时遇到困难。

当我在浏览器中输入以下行并按Enter键时,Windows Contacts应用程序将显示来自服务器的记录,因此我知道这是连接到的正确位置:

  

LDAP://directory.abc.edu/uid=asmith,ou=People,o=abc.edu

但是当我想在代码中使用相同的东西时,我得到“无效的dn语法”错误消息。

这是我的代码:

public void LDAPResult()
        {           
            using (DirectoryEntry root = new DirectoryEntry(string.Format(@"LDAP://directory.abc.edu/uid=asmith,ou=People,o=abc.edu")))
            {
                using (DirectorySearcher searcher = new DirectorySearcher(root))
                {
                    //This following line give me the error
                    **SearchResultCollection results = searcher.FindAll();**

//The rest is not actually important, I never get there to see if it works properly.
                    StringBuilder summary = new StringBuilder();
                    foreach (SearchResult result in results)
                    {
                        foreach (string propName in result.Properties.PropertyNames)
                        {
                            foreach (string s in result.Properties[propName])
                            {
                                summary.Append(" " + propName + ": " + s + "\r\n");
                            }
                        }
                        summary.Append("\r\n");
                    }
                    Console.WriteLine(summary);
                }
            }            
        }

对此的任何帮助都非常感谢。 谢谢,

2 个答案:

答案 0 :(得分:1)

我不确定您要连接的LDAP目录,但您的DN看起来不太正确。

特别是“o = abc.edu”部分。在Active Directory(我最熟悉的目录)中,DN最终将成为uid = asmith,ou = People,dc = abc,dc = edu。请注意,abc和edu是截然不同的部分。由于您使用的是O而不是DC,我猜测该目录不是AD,但域名的部分可能仍然使用两个o来表示。 o = abc,o = edu也许?

答案 1 :(得分:0)

你应该看看这里

Connecting to LDAP from C# using DirectoryServices

在这里

LDAP Directory Entry in .Net - not working with OU=Users

特别是对于“new DirectoryEntry(...)”用法:)

相关问题