在我的系统中,客户端告知电子邮件服务的配置,该服务负责在系统内传递和接收日志通信。当我尝试在 ASP.NET 网站内使用 MailKit 从生产 Windows 2008 R2 服务器测试与 SSL IMAP 服务器的连接时,出现此错误:
异常:System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效。 System.Security.Cryptography.X509Certificates.X509ChainStatus 状态:RevocationStatusUnknown -“吊销功能无法检查证书的吊销”
当我尝试从我的电脑或虚拟机 Windows 2008 R2 服务器连接时,连接成功。而且,当我尝试通过命令行从生产服务器连接 OpenSSL.dll 时,连接成功。我使用相同的代码制作了一个控制台应用程序,没有任何更改,并且该应用程序可以连接到 IMAP 服务器,但是,当从网站或服务进行连接时,会引发错误。
这是代码:
//test method
public bool Testar()
{
try
{
using (var client = new MailKit.Net.Imap.ImapClient(new MailKit.ProtocolLogger("IMAP.log")))
{
client.ServerCertificateValidationCallback = VerificarErrosCertificadosServer;
client.Connect(host, this.IsSSl);
client.Authenticate(emailUserName, emailPassword);
var inbox = client.Inbox;
inbox.Open(folderAccess);
client.Disconnect(true);
return true;
}
}
catch (Exception ex)
{
Log.Error(ex);
return false;
}
}
//VerificarErrosCertificadosServer method
private static bool VerificarErrosCertificadosServer(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
{
return true;
}
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
{
Log.Info("System.Security.Cryptography.X509Certificates.X509ChainStatus status: " + status.Status.ToString() + " - " + status.StatusInformation);
if ((certificate.Subject == certificate.Issuer) &&
(status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// Self-signed certificates, untrusted root, but valid.
continue;
}
else
{
if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// any error
return false;
}
}
}
}
return true;
}
else
{
return false;
}
}
有任何想法吗?