2012-09-21 49 views
0

我很难编写一个可以执行LDAP认证的模块。ldap认证dn语法错误

当我把下面一行在我的浏览器并按下回车键,Windows联系人应用程序会告诉我来自服务器的纪录,所以我知道这是正确的位置连接到:

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

任何帮助,这是如此高度赞赏。 谢谢,

回答

1

我不确定你要连接到哪个LDAP目录,但是你的DN看起来并不是很理想GHT。

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

+0

很对。埃米尔,你真的把一个点放入了“o =”级对象的RDN吗? –

+0

嗯,这是一个很好的观点。我解决了这个问题,但仍然得到了同样的错误,“dn语法错误”,但后来我将其更改为以下格式并更改了错误消息。 我现在使用的格式是: using(DirectoryEntry root = new DirectoryEntry(string.Format(@“LDAP://CN=directory.gmu.edu,OU=People,DC=gmu,DC=edu”)) ) 现在,当我运行程序需要一段时间,然后它说“从服务器返回的引荐”任何人都可以告诉我这意味着什么,我现在该做什么? 非常感谢您的帮助John。 –

+1

根据DirectorySearching类的文档,您传递给搜索器的目录条目是搜索的根目录。所以你可能只想通过“LDAP:// OU = People,DC = gmu,DC = edu”。人们可能是一个Container而不是一个OU,所以你可能也想尝试一下“LDAP:// CN = People,DC = gmu,DC = edu”。如果您正在搜索AD域控制器,则用户的默认位置实际上是CN = Users,因此您也可以尝试“LDAP:// CN = users,dc = gmu,dc = edu”。 –