首页 > 解决方案 > 刚刚更新 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)"
      }

.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;

标签: asp.netssl

解决方案


经过大量测试和研究后,我尝试修复了所有问题。似乎 .NET 中的某些东西正在缓存 SSL 证书。

我做的步骤:

  • 运行 windows 更新(有一些更新要运行)。这给了我与上面不同的错误消息,但它仍然包含有关 SSL 信任问题的错误。
  • 检查注册表中是否启用了 SSL 和 TLS(它们是)。重新启动,同样的错误,
  • 清除 .net 缓存,重新部署,但仍然失败。
  • 重新安装了 ASP.NET,从 MMC(计算机/用户)中删除了证书,运行了一些 PS 脚本来杀死 IIS 缓存,将证书添加为 PFX 而不是 .CER,并且它起作用了。

推荐阅读