gpt4 book ai didi

ssl - ASP.NET 和底层连接已关闭 : Could not establish trust relationship for the SSL/TLS secure channel

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:25 25 4
gpt4 key购买 nike

我正在使用公共(public)根权限证书文件 X509 进行 httpweb 请求。我只有公钥,没有私钥。在控制台应用程序中一切正常,但在 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 信息: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;
}

我最终得到了错误:

Remote Certificate Chain Error

最佳答案

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

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

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

来源:

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

注意:不建议将此解决方法用于生产

关于ssl - ASP.NET 和底层连接已关闭 : Could not establish trust relationship for the SSL/TLS secure channel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2022187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com