asp.net - 刚刚更新 SSL 证书,我得到“底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。”,
问题描述
我刚刚为我们的网站更新了 SSL 证书。我已经这样做了多年,没有任何问题。
在最后一次 SSL 更新之后,当我的网站相互通信时出现错误,但是当我通过 chrome 或其他浏览器单独浏览每个站点时没有错误。
如果我通过浏览器浏览站点 A 或站点 B,则没有错误。但是,如果我转到站点 A 需要通过 HTTPRequest 访问站点 B 的页面,则会收到以下错误:
"message": "An error has occurred.",
"exceptionMessage": "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.",
"exceptionType": "System.Net.WebException",
"stackTrace": " at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)\r\n at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "The remote certificate is invalid according to the validation procedure.",
"exceptionType": "System.Security.Authentication.AuthenticationException",
"stackTrace": " at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)\r\n at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)"
}
- 两个站点都驻留在同一台服务器上
- 两个站点具有相同的根域名。
(例如 SiteA.foo.com、siteB.foo.com) - 服务器 2012。
- 我从生产服务器执行了 SSL 请求,并在生产服务器上完成了它。
- 我的本地开发人员或我们的其他堆栈(使用相同的证书)上没有这个问题
- 从所有商店(MMC)中删除了新证书,重新启动。将 PFX 安装到两个用户/计算机帐户上。重新绑定 IIS 中的所有站点。浏览器中的每个站点都“有效”,但是当您尝试让一个后端调用另一个网站时,我会遇到同样的错误。
.NET 框架 - v4.0.30319,管道 - 集成
查看 2017-2018 证书,除了您所期望的之外,没有任何变化。主题、域等都是一样的。
这是一段正在爆炸的代码。
using (var handler = new HttpClientHandler())
{
HttpResponseMessage message = null;
handler.CookieContainer = _cookieContainer;
handler.UseDefaultCredentials = false;
handler.UseCookies = true;
using (var client = new HttpClient(handler))
{
var attempt = 0;
client.Timeout = _requestTimeout;
var uri = BuildActionUri(baseUri, action);
while (attempt++ < retryAttempts)
{
HttpContent httpContent;
switch (actionType)
{
case ActionType.Put:
httpContent = authenticationProvider.SignRequest(client, request, grantResponse);
message = client.PutAsync(uri, httpContent).Result;
break;
case ActionType.PostAsJson:
authenticationProvider.SignRequest(client, new EntityJsonRequest {Entity = data}, grantResponse);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
message = client.PostAsJsonAsync(uri, data).Result;
break;
case ActionType.Post:
httpContent = authenticationProvider.SignRequest(client, request, grantResponse);
message = client.PostAsync(uri, httpContent).Result;
break;
case ActionType.PutAsJson:
authenticationProvider.SignRequest(client, new EntityJsonRequest {Entity = data},
grantResponse);
message = client.PutAsJsonAsync(uri, data).Result;
break;
case ActionType.Get:
authenticationProvider.SignRequest(client, null, grantResponse);
message = client.GetAsync(uri).Result;
break;
解决方案
经过大量测试和研究后,我尝试修复了所有问题。似乎 .NET 中的某些东西正在缓存 SSL 证书。
我做的步骤:
- 运行 windows 更新(有一些更新要运行)。这给了我与上面不同的错误消息,但它仍然包含有关 SSL 信任问题的错误。
- 检查注册表中是否启用了 SSL 和 TLS(它们是)。重新启动,同样的错误,
- 清除 .net 缓存,重新部署,但仍然失败。
- 重新安装了 ASP.NET,从 MMC(计算机/用户)中删除了证书,运行了一些 PS 脚本来杀死 IIS 缓存,将证书添加为 PFX 而不是 .CER,并且它起作用了。
推荐阅读
- outlook - 跨林环境中的日历回复
- dns - IP地址和域名的区别
- r - 为什么在本地开源 RStudio 上运行的简单闪亮应用程序的加载时间变化很大?
- apache-spark - 如何从 Spark Structured Streaming 刷新 Hive/Impala 表?
- c++ - 在 C++ 中的另一个函数中定义一个函数
- sql - SELECT * 比 SELECT columnlist 快
- xml - 如何将 POST azure api 管理系统中的输入参数转换为 XML 并使其成为请求正文?
- indexing - 如何在维护现有索引的同时执行多个左连接?
- mysql - 调用 APIMAN 的 API 时出错
- python - 如何对列表中的嵌套字典进行排序?