C# LdapConnection和LDAPS出现错误0x80005000
在我开始之前,我已经访问并更改了我的代码,虽然它确实解决了问题,但它似乎神秘地回来了 好消息是:C# LdapConnection和LDAPS出现错误0x80005000,c#,asp.net,ldap,ldapconnection,C#,Asp.net,Ldap,Ldapconnection,在我开始之前,我已经访问并更改了我的代码,虽然它确实解决了问题,但它似乎神秘地回来了 好消息是: public static bool Authenticate(string username, string password, string domain) { bool authentic = false; try { LdapConnection con = new LdapConnection( new LdapDirect
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_NativeObject()+31
Complete.Authentication.GCAuthentication.Authenticate(字符串用户名、字符串密码、字符串域)在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.Open\Complete.AuthenticationFactory.ValidateUserLdap(字符串用户名、字符串密码、字符串域、布尔值isValid、字符串用户名WithDomain)中
这是相当令人困惑的,因为似乎有些用户帐户工作,而另一些则不工作。然而,当我把上面的代码放在一个独立的测试环境中时,不管我使用哪个帐户,它每次都会成功。当我使用ASP.NET和IIS将其放回Windows 2008 R2服务器时,它会如上所述失败。但失败是一致的——账户总是失败或成功,从这个角度来看,不存在随机性
必须使用LDAPS而不是LDAP访问LDAP服务器,这就是为什么我们不能使用DirectoryEntry对象的原因-LDAP服务器由客户端控制,因此不能以任何方式重新配置或更改。我们只想在web表单上捕获用户名/密码,然后在LDAP服务器上使用BIND检查凭据
我们使用的是.NET 3.5,目前无法升级,因此我敬请您,如果您的主要建议和论点是升级,请推迟您的贡献
谢谢,希望你能帮上忙。想为你做点什么吗
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);
这里是一个非常好的网站,提供了大量的参考和示例
对于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;
}
Karell,您如何连接到LDAP,您能否使用(DirectoryEntry de=new DirectoryEntry()){de.Path=“LDAP://yourSite/rootDSE”;de.Username=@“domain\Karell”;de.Password=“Password”;}显示LDAP连接字符串,例如LDAP://…fore example
sure thing:LDAPS://ServerAddress/OU=Users,DC=domain,DC=com-我们没有在用户名上指定域(从来没有问题,在“工作”帐户上工作)。这可能是字符编码问题,非工作用户dn或pwd包含特殊字符(逗号、utf-8、引号)?我可以偷看一个帐户和密码,它只包含字母/数字字符-但感谢您的建议谢谢DJ,但似乎这种技术-即使我指定端口636(LDAPS)实际上也不会通过LDAP使用SSL。测试方法Complete.Authentication.Tests.GCAuthenticationTests.Test_AuthenticationSsl引发异常:System.DirectoryServices.AccountManagement.PrincipalServerDownException:无法联系服务器。-->System.DirectoryServices.Protocols.LdapException:LDAP服务器不可用。您已尝试将端口更改为636。很抱歉,我忘了您是通过SSL执行此操作的。您可以发布LDAP://connect的确切字符串以更改IP,但将所有其他相关内容与:636端口和其他内容一起放置吗。我想看一些东西,我也通过指定端口尝试过,问题是这个LDAP服务器只能在SSL/636上访问。如果我指定636,它只是切换端口,但没有使用正确的协议。这很奇怪。。你能找到他们是否有允许SSL的设置吗。。?你能找到吗..这个设置已经运行了2个月-然后在其他用户继续工作时,大多数用户开始失败。