在开始之前,我已经使用 LDAPS 连接访问了未知错误 (0x80005000)并更改了我的代码,虽然它确实解决了问题,但它似乎神秘地回来了。
这是好东西:
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(Boolean throwIfFail) +378094 System.DirectoryServices.DirectoryEntry.Bind() +36 System.DirectoryServices.DirectoryEntry.get_NativeObject() +31 Complete.Authentication .GCAuthentication.Authenticate(String username, String password, String domain) 在 c:\Builds\6\Idealink.Open.Pancanal\Panama Canal\Sources\Idealink.Open\Complete.Authentication\GCAuthentication.cs:27 Complete.Authentication。 AuthenticationFactory.ValidateUserLdap(String username, String password, String domain, Boolean isValid, String usernameWithDomain) 在 c:\Builds\6\Idealink.Open.Pancanal\Panama Canal\Sources\Idealink.Open\Complete.Authentication\AuthenticationFactory.cs: 93
这很令人困惑,因为似乎某些用户帐户有效而其他用户帐户无效。但是,当我将上述代码放在一个隔离的测试环境中时,它每次都会成功,无论我使用哪个帐户。当我将它放回带有 ASP.NET 和 IIS 的 Windows 2008 R2 服务器时,它如上所述失败。但失败是一致的——账户总是失败或成功,从这个角度来看,没有随机性。
LDAP 服务器必须使用 LDAPS 而不是 LDAP 访问,这就是我们不能使用 DirectoryEntry 对象的原因 - LDAP 服务器由客户端控制,因此不能以任何方式重新配置或更改。我们只是想在 Web 表单上捕获用户名/密码,然后在 LDAP 服务器上使用 BIND 来检查凭据。
我们正在使用 .NET 3.5,目前无法升级,所以我恭敬地询问,如果您的主要建议和论点是升级,那么请推迟您的贡献。
谢谢,希望能帮到你