2013-01-08 127 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,它只是切换端口,但不使用正确的协议。 –

相关问题