2013-01-08 129 views
0

在我開始之前,我已經訪問Unknown Error (0x80005000) with LDAPS Connection並更改我的代碼,雖然它確實解決了這個問題,似乎它有神祕地回來。錯誤0x80005000與LdapConnection和LDAPS

這裏的好東西:

public static bool Authenticate(string username, string password, string domain) 
{ 
    bool authentic = false; 

    try 
    { 
     LdapConnection con = new LdapConnection(
      new LdapDirectoryIdentifier(Host, Port)); 
     if (IsSSL) 
     { 
      con.SessionOptions.SecureSocketLayer = true; 
      con.SessionOptions.VerifyServerCertificate = ServerCallback; 
     } 
     con.Credential = new NetworkCredential(username, password); 
     con.AuthType = AuthType.Basic; 
     con.Bind(); 
     authentic = true; 
    } 
    catch (LdapException) 
    { 
     return false; 
    } 
    catch (DirectoryServicesCOMException) 
    { } 
    return authentic; 
} 

public static bool IsSSL 
{ 
    get 
    { 
     return ConnectionString.ToLower().Contains("ldaps"); 
    } 
} 

public static string ConnectionString 
{ 
    get 
    { 
     if (string.IsNullOrEmpty(_connectionString)) 
      _connectionString = CompleteConfiguration.GetLDAPConnectionString(); 

     return _connectionString; 
    } 
    set { _connectionString = value; } 
} 

public static int Port 
{ 
    get 
    { 
     var x = new Uri(ConnectionString); 
     int port = 0; 
     if (x.Port != -1) 
     { 
      port = x.Port; 
     } 
     else 
     { 
      port = x.OriginalString.ToLower().Contains("ldaps") 
         ? 636 
         : 389; 
     } 
     return port; 
    } 
} 

public static string Host 
{ 
    get 
    { 
     var x = new Uri(ConnectionString); 
     return x.Host; 
    } 
} 

private static bool ServerCallback(LdapConnection connection, X509Certificate certificate) 
{ 
    return true; 
} 

這裏是壞的東西: 當我嘗試驗證的應用程序,我碰到下面的錯誤,更確切地說,這是由con.Bind()觸發線:

[收到COMException(0x80005000):未知的錯誤(0x80005000)] System.DirectoryServices.DirectoryEntry.Bind(布爾throwIfFail)378094 System.DirectoryServices.DirectoryEntry.Bind()36 System.DirectoryServices.DirectoryEntry。 get_NativeOb ject()+31 Complete.Authentication.GCAuthentication.Authenticate(String username,String password,String domain)in c:\ Builds \ 6 \ Idealink.Open.Pancanal \ Panama Canal \ Sources \ Idealink.Open \ Complete.Authentication \ GCAuthentication.cs:27 c:\ Builds \ 6 \ Idealink.Open.Pancanal \ Panama Canal \ Sources \ Idealink中的Complete.Authentication.AuthenticationFactory.ValidateUserLdap(String username,String password,String domain,Boolean isValid,String usernameWithDomain)。打開\ Complete.Authentication \ AuthenticationFactory.cs:93

這是相當混亂,因爲它似乎有些用戶帳戶工作,而其他人不。但是,當我將上述代碼放置在獨立的測試環境中時,無論我使用哪個帳戶,它都會成功。當我將它放回到帶有ASP.NET和IIS的Windows 2008 R2服務器上時,它將失敗,如上所述。儘管失敗是一致的 - 賬戶始終失敗或成功,從這個角度來看,沒有任何隨機性。

必須使用LDAPS和NOT LDAP訪問LDAP服務器,這就是爲什麼我們不能使用DirectoryEntry對象 - LDAP服務器由客戶端控制,因此無法以任何方式重新配置或更改。我們只是想在Web表單上捕獲用戶名/密碼,然後在LDAP服務器上使用BIND來檢查憑證。

我們正在使用.NET 3.5,目前無法升級,因此我恭敬地問,如果您的主要建議和參數要升級,請暫緩您的貢獻。

謝謝,希望你能幫助

+0

Karell,怎麼是一個非常好的網站您連接到LDAP,您可以顯示LDAP連接字符串,例如LDAP:// ... – MethodMan

+0

示例'使用(DirectoryEntry de = new DirectoryEntry())de.Path =「LDAP:// yourSite/rootDSE」; de.Username = @「domain \ Karell」; de。密碼=「密碼」;}' – MethodMan

+0

肯定的事情:LDAPS:// ServerAddress/OU = Users,DC = domain,DC = com - 我們沒有在用戶名上指定域名(從來沒有問題, 「工作」賬戶)。 –

回答

2

會是這樣的工作你..?

const string Domain = "ServerAddress:389"; 
const string constrParts = @"OU=Users,DC=domain,DC=com"; 
const string Username = @"karell"; 
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain, constrParts); 
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username); 

這裏是一個偉大的參考和示例 DirectoryServices DirectoryEntry

通過SSL連接,你可以不喜歡以下

const int ldapInvalidCredentialsError = 0x31; 
const string server = "your_domain.com:636"; 
const string domain = "your_domain.com"; 

try 
{ 
    using (var ldapSSLConn = new LdapConnection(server)) 
    { 
     var networkCredential = new NetworkCredential(username, password, domain); 
     ldapSSLConn.SessionOptions.SecureSocketLayer = true; 
     ldapSSLConn.AuthType = AuthType.Negotiate; 
     ldapSSLConn.Bind(networkCredential); 
    } 

    // If the bind succeeds, the credentials are valid 
    return true; 
} 
catch (LdapException ldapEx) 
{ 
    // Invalid credentials a specific error code 
    if (ldapEx.ErrorCode.Equals(ldapInvalidCredentialsError)) 
    { 
     return false; 
    } 

    throw; 
} 

MSDN list of Invalid LDAP Error Codes

+0

感謝DJ,但似乎這種技術 - 即使我指定端口636(LDAPS)實際上不會使用SSL通過LDAP。 測試方法Complete.Authentication.Tests.GCAuthenticationTests.Test_AuthenticationSsl拋出異常: System.DirectoryServices.AccountManagement.PrincipalServerDownException:無法聯繫服務器。 ---> System.DirectoryServices.Protocols.LdapException:LDAP服務器不可用。 –

+0

你已經嘗試將此端口更改爲636我很抱歉我忘了你是通過SSL來完成這項工作的,你可以發佈LDAP的確切字符串://連接改變IP,但將所有其他相關的東西放在:636端口和東西。我想看看 – MethodMan

+0

我也嘗試過通過指定端口,問題是這個LDAP服務器只能在SSL/636上訪問。如果我指定636,它只是切換端口,但不使用正確的協議。 –

相關問題