ASP.NET和底层连接被关闭:无法建立SSL/TLS安全通道的信任关系

10

我正在使用公共根授权证书文件X509进行httpwebrequest。我只有公钥,没有私钥。在控制台应用程序中一切正常,但在asp.net应用程序中无法工作。我收到错误消息:“基础连接被关闭:无法为SSL/TLS安全通道建立信任关系。”

禁用验证的选项不是一个选择。

以下是代码:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://xxxxxxx/gateway.aspx");
string post = "abcdef";
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.ContentLength = post.Length;

var cert = System.Security.Cryptography.X509Certificates.X509Certificate.CreateFromCertFile(@"c:\temp\root.cer");

req.ClientCertificates.Add(cert);
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(post.ToString());
stOut.Close();

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

以下是来自System.Net和System.Net Sockets的系统日志。

System.Net信息: 0 : [5928] SecureChannel#8106798 - 无法建立到受信任根机构的证书链。

System.Net信息: 0 : [5928] SecureChannel#8106798 - 远程证书被用户验证为无效。

System.Net.Sockets详细信息: 0 : [5928] Socket#7486778 :: Dispose()

System.Net错误: 0 : [5928] HttpWebRequest#51319244 :: 中出现异常-无法为SSL / TLS安全通道建立信任关系的基础连接已关闭。

System.Net错误: 0 : [5928] HttpWebRequest#51319244 :: EndGetRequestStream中出现异常-无法为SSL / TLS安全通道建立信任关系的基础连接已关闭。

更多信息:

如果我使用这段代码(来自CodeGuru)

  public static bool ValidateServerCertificate(object sender,
     X509Certificate certificate, X509Chain chain,
     SslPolicyErrors sslPolicyErrors)
  {
     if (sslPolicyErrors ==
        SslPolicyErrors.RemoteCertificateChainErrors) {
        return false;
     } else if (sslPolicyErrors ==
        SslPolicyErrors.RemoteCertificateNameMismatch) {
        System.Security.Policy.Zone z =
           System.Security.Policy.Zone.CreateFromUrl
           (((HttpWebRequest)sender).RequestUri.ToString());
        if (z.SecurityZone ==
           System.Security.SecurityZone.Intranet ||
           z.SecurityZone ==
           System.Security.SecurityZone.MyComputer) {
           return true;
        }
        return false;
     }
     return true;
  }

我最终遇到了这个错误:

远程证书链错误

2个回答

13

1

听起来问题可能是与这个链接发布的内容有关。ASPNET工作进程要求证书名称与服务器名称匹配。可以在测试环境中实现解决方法。

对于证书生成,您可以使用一个名为SelfSSL.exe的免费程序,并使用以下命令:

SelfSSL.exe /T /N:CN=localhost /V:999 /Q(其中“localhost”是证书名称)

和:

winHTTPCertCfg.exe -g -c local_machine\my -s localhost -a Administrators(以授予管理员访问证书)


已使用MMC快照将root.cer添加到了LocalMachine存储。我还尝试使用winhttpcertcfg.exe,但没有成功。因为我既没有私钥,也没有PFX文件。有什么想法吗? - Jeffrey GAdoury
测试环境没有使用SSL,所以这没问题。我原本计划使用CA的根证书,但那可能是问题的一部分。我想服务器端的SSL设置有点不可靠。 - Jeffrey GAdoury

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接