2012-09-21 79 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」。 –