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

发布于 2024-08-16 19:02:40 字数 2205 浏览 4 评论 0 原文

我正在使用公共根权限证书文件 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 套接字的系统日志。

System.Net Information: 0 : [5928] SecureChannel#8106798 - 证书链无法构建为受信任的根权限。

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

System.Net.Sockets Verbose: 0 : [5928] Socket#7486778::Dispose()

System.Net Error: 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;
  }

我最终会收到错误:

远程证书链错误

I'm making an httpwebrequest using a public Root authority Certificat file X509. I only have the public key, not the private key. Everything works fine from a Console app but it does not work from an asp.net app. I get the error: "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

The option to disable Validation is not an option.

Here is the code

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();

Here is the System Logs from System.Net and System.Net Sockets.

System.Net Information: 0 : [5928] SecureChannel#8106798 - A certificate chain could not be built to a trusted root authority.

System.Net Information: 0 : [5928] SecureChannel#8106798 - Remote certificate was verified as invalid by the user.

System.Net.Sockets Verbose: 0 : [5928] Socket#7486778::Dispose()

System.Net Error: 0 : [5928] Exception in the HttpWebRequest#51319244:: - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

System.Net Error: 0 : [5928] Exception in the HttpWebRequest#51319244::EndGetRequestStream - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

Further info

If I use this code (from 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;
  }

I ultimately get the error:

Remote Certificate Chain Error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

依 靠 2024-08-23 19:02:40

这个问题可以通过添加到 Application_Start 来解决:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

它基本上允许服务器与其证书之间不匹配。

资料来源:

http: //www.ben-morris.com/asp-net-web-services-and-ssl-certificates-builting-a-trust-relationship

注意:不建议在生产中使用此解决方法

This problem can be fixed with this added to Application_Start:

ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

It basically allows for a mismatch between the server and its certificate.

Source:

http://www.ben-morris.com/asp-net-web-services-and-ssl-certificates-establishing-a-trust-relationship

Note: This workaround is not recommended for production

谁与争疯 2024-08-23 19:02:40

听起来问题可能出在此 链接。 ASPNET 工作进程需要证书名称与服务器名称匹配。有一些解决方法可以在测试环境中实现。

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

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

以及:

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

It sounds like the issue could be what is posted on this link. The ASPNET worker process requires the cert name to match the server name. There are work arounds that can be implemented with test environments.

For certificate generation you can use a free program called SelfSSL.exe with commands such as:

SelfSSL.exe /T /N:CN=localhost /V:999 /Q (where "localhost" is cert name)

And:

winHTTPCertCfg.exe -g -c local_machine\my -s localhost -a Administrators (to grant admins access to the cert)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文