2013-02-19 21 views
1

我有一些正在運行的LDAP代碼,我們使用其專有名稱重新綁定到找到的用戶以驗證用戶。實際上,這是發生了什麼事:OpenLdap C#使用可分辨名稱中的轉義字符進行綁定

  string userDn = @"cn=Feat Studentl+umanroleid=302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB"; 
      string fullPath = @"LDAP://surinam.testsite.ac.uk:636/" + userDn; 

      DirectoryEntry authUser = new DirectoryEntry(fullPath, userDn, "mypassword", AuthenticationTypes.None); 

      authUser.RefreshCache(); 

然而這會導致錯誤未知錯誤80005000在DirectoryEntry.Bind()

我懷疑問題可能是,DN有一個「+」和「=」在CN屬性中。因此發現逃避這個問題的方法應該是用\和字符的十六進制值之後,我嘗試這樣做:

  string userDn = @"cn=Feat Studentl\2Bumanroleid\3D302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB"; 

但是我得到的錯誤:

登錄失敗:未知的用戶名或錯誤密碼

我認爲這是因爲現在它對請求感到滿意,但由於某種原因,它無法匹配用戶DN。

反正有這個嗎?

回答

1

根據我的經驗,開發LDAP服務時,無論憑證是否無效都會導致登錄失敗,確實往往是綁定嘗試的問題。你得到這個錯誤是因爲DirectoryEntry不能解析DN中的轉義字符......但是,你不應該首先這樣做。

在您的代碼中 - 將AuthenticationTypes設置爲「None」將強制條目根據您提供的DN進行簡單綁定。由於您的包括服務器名稱爲路徑的一部分,我會嘗試使用ServerBind認證類型,而不是像這樣:

string LdapPath = ("LDAP://" + ldapUrl + "/" + Domain); 

//Build the user and issue the Refresh bind 
var dirEntry = new DirectoryEntry 
        { 
         Path = LdapPath, 
         Username = _usernameToVerify, 
         Password = _passwordToVerify, 
         AuthenticationType = AuthenticationTypes.ServerBind 
        }; 

//This will load any available properties for the user 
dirEntry.RefreshCache(); 

此外,它看起來像你在進行此調用的安全LDAP端口(636 ),所以請確保您還包括AuthenticationTypes.SecureSocketsLayer以及ServerBind機制:

AuthenticationType = AuthenticationTypes.ServerBind | AuthenticationTypes.SecureSocketsLayer 

希望這有助於!

+0

感謝Gregory。我嘗試了你的例子,我相信它會幫助別人,但對我來說,它仍然在內部發生了80005000錯誤。我已經發布了一個較低級別的DirectoryServices示例,我已設法開始工作。 – user1444886 2013-02-20 13:05:58

0

我不得不求助於挖掘一個爲一個客戶定製的舊DLL項目。

我設法讓它工作。看起來您必須參考這些低級別的目錄服務例程(如果您的DN帶有轉義字符)。 (請注意在現實生活中,DN是通過設置DirectorySearcher並首先執行FindOne進行初始靈活用戶搜索獲得的)

string userDn = @"cn=Feat Studentl+umanroleid=302432,ou=Faculty of Engineering & Physical Sciences Administration,ou=Faculty of Engineering & Physical Sciences,ou=People,o=University of TestSite,c=GB"; 
string basicUrl = @"surinam.testsite.ac.uk:636"; 



    var ldapConnection = new LdapConnection(basicUrl); 
    ldapConnection.AuthType = AuthType.Basic; 
    LdapSessionOptions options = ldapConnection.SessionOptions; 
    options.ProtocolVersion = 3; 
    options.SecureSocketLayer = true; 

    NetworkCredential credential = new NetworkCredential(userDn, password);        
    ldapConnection.Credential = credential; 

    try 
    { 
     ldapConnection.Bind(); 
     Console.WriteLine("bind succeeded "); 
    } 
    catch (LdapException e) 
    { 
     if (e.ErrorCode == 49) 
     { 
      Console.WriteLine("bind failed "); 
     } 
     else 
     { 
      Console.WriteLine("unexpected result " + e.ErrorCode); 
     } 
    } 
    catch (DirectoryOperationException e) 
    { 
     Console.WriteLine("unexpected error " + e.Message); 
    } 
相關問題