当前位置:   article > 正文

Issue—C#—ssl相关错误_the ssl connection could not be established, see i

the ssl connection could not be established, see inner exception.

报错:

.net 6 :

{

"MessageTemplate": "An unhandled exception has occurred while executing the request."

"Exception": "System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.\r\n ---> System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'.\r\n ---> System.ComponentModel.Win32Exception (0x80090326): 接收到的消息异常,或格式不正确。\r\n --- End of inner exception stack trace ---\r\n at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)\r\n at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(HttpRequestMessage request)\r\n at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)\r\n at System.Net.Http.HttpConnectionPool.GetHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)\r\n at Oxlias.Validation.ZhxtTokenAuthenticationHandler.GetZhxtTokenData(String uri, HttpMethod httpmethod, String zhxttoken)\r\n at Oxlias.Validation.ZhxtTokenAuthenticationHandler.AuthenticateAsync()\r\n at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at Hellang.Middleware.ProblemDetails.ProblemDetailsMiddleware.Invoke(HttpContext context)"

}

.net framework 4.6.2 :未能创建 SSL/TLS 安全通道。终止连接。


一、通过代码解决

这种只适合Client与Server双方服务器的TLS协议版本号兼容,还需要Cipher Suites(密码套件)兼容的前提下。

  1. httpclient写法

Product

Versions

.NET

Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7

.NET Framework

4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

.NET Standard

1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1

UWP

10.0

Xamarin.iOS

10.8

Xamarin.Mac

3.0

  1. private async Task<string> GetZhxtTokenData(string uri)
  2. {
  3. //HttpClientHandler clientHandler = new HttpClientHandler();
  4. //clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
  5. //clientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls11;
  6. //HttpClient client = new HttpClient(clientHandler);
  7. //var request = new HttpRequestMessage
  8. //{
  9. // Method = httpmethod,
  10. // RequestUri = new(uri, UriKind.Absolute)
  11. //};
  12. //request.Headers.Add("Authorization", zhxttoken);
  13. //var gettoken_Response = await client.SendAsync(request);
  14. //return await gettoken_Response.Content.ReadAsStringAsync();
  15. using (var clientHandler = new HttpClientHandler())
  16. {
  17. clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
  18. clientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls11;
  19. using (var httpClient = new HttpClient(clientHandler))
  20. {
  21. httpClient.DefaultRequestHeaders.Add("Authorization", "123");
  22. using (var response = await httpClient.GetAsync(uri))
  23. {
  24. if (response.StatusCode == HttpStatusCode.RequestTimeout)
  25. {
  26. return HttpStatusCode.RequestTimeout.ToString();
  27. }
  28. return await response.Content.ReadAsStringAsync();
  29. }
  30. }
  31. }
  32. }
  1. HttpWebRequest写法

  1. public static string GetZhxtTokenData_HttpWebRequest(string PostUrl, string Parameters, string zhxttoken)
  2. {
  3. string content = string.Empty;
  4. try
  5. {
  6. //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Ssl3; //https 请求必需语句,http 请求可省略
  7. //跳过ssl验证
  8. ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
  9. //path不是登录界面,是登录界面向服务器提交数据的界面
  10. HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(PostUrl);
  11. myReq.Method = "get";
  12. //myReq.ContentType = "application/json";
  13. //myReq.Connection = "keep-alive";
  14. myReq.Headers.Add("Cookie", "477cadb5a1f917cda5f5d9cab20f8841=42c7f20d7a509babe62c3e74995d5140; 3c343960f5dd4584c34cd6b38885cefe=000e1bf2bbd8e5ae2dabff0eb27e3b6f");
  15. //myReq.Headers.Add("usercode", "JRFZCX_MYFK");
  16. //myReq.Headers.Add("password", "*&ZHY&*0379");//&JRFZ&01
  17. myReq.Headers.Add("Authorization", zhxttoken);
  18. //填充POST数据
  19. if (Parameters != null)
  20. {
  21. //转换为字节数组
  22. byte[] bytesRequestData = Encoding.UTF8.GetBytes(Parameters);
  23. myReq.ContentLength = bytesRequestData.Length;
  24. Stream requestStream = myReq.GetRequestStream();
  25. requestStream.Write(bytesRequestData, 0, bytesRequestData.Length);
  26. requestStream.Close();
  27. }
  28. else
  29. {
  30. myReq.ContentLength = 0;
  31. }
  32. //发送POST数据请求服务器
  33. HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
  34. //获取服务器返回信息
  35. Stream myStream = HttpWResp.GetResponseStream();
  36. StreamReader reader = new StreamReader(myStream, Encoding.UTF8);
  37. content = reader.ReadToEnd();
  38. reader.Close();
  39. HttpWResp.Close();
  40. }
  41. catch (Exception ex)
  42. {
  43. content = ex.ToString();
  44. }
  45. return content;
  46. }

二、通过服务期配置解决

1.Analysis

当通过代码手段无法解决时需从问题根源寻找解决办法。

根据错误提示“System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'. System.ComponentModel.Win32Exception (0x80090326): 接收到的消息异常,或格式不正确。”分析出应该是两个服务器TLS版本号不兼容导致两边通信建立连接时三次握手失败。

①经查看服务器A(Red hat8.1-5) ssl密码件如下:

  1. sh-4.4$ cat DEFAULT.pol
  2. # A reasonable default for today's standards. It should provide
  3. # 112-bit security with the exception of SHA1 signatures needed for DNSSec
  4. # and other still prevalent legacy use of SHA1 signatures.
  5. # MACs: all HMAC with SHA1 or better + all modern MACs (Poly1305 etc)
  6. # Curves: all prime >= 255 bits (including Bernstein curves)
  7. # Signature algorithms: with SHA-1 hash or better (no DSA)
  8. # TLS Ciphers: >= 128-bit key, >= 128-bit block (AES, ChaCha20, including AES-CBC)
  9. # non-TLS Ciphers: as TLS Ciphers with added Camellia
  10. # key exchange: ECDHE, RSA, DHE (no DHE-DSS)
  11. # DH params size: >= 2048
  12. # RSA params size: >= 2048
  13. # TLS protocols: TLS >= 1.2, DTLS >= 1.2
  14. mac = AEAD HMAC-SHA2-256 HMAC-SHA1 UMAC-128 HMAC-SHA2-384 HMAC-SHA2-512
  15. group = X25519 X448 SECP256R1 SECP384R1 SECP521R1 \
  16. FFDHE-2048 FFDHE-3072 FFDHE-4096 FFDHE-6144 FFDHE-8192
  17. hash = SHA2-256 SHA2-384 SHA2-512 SHA3-256 SHA3-384 SHA3-512 SHA2-224 SHA1
  18. sign = ECDSA-SHA3-256 ECDSA-SHA2-256 \
  19. ECDSA-SHA3-384 ECDSA-SHA2-384 \
  20. ECDSA-SHA3-512 ECDSA-SHA2-512 \
  21. EDDSA-ED25519 EDDSA-ED448 \
  22. RSA-PSS-SHA2-256 RSA-PSS-SHA2-384 RSA-PSS-SHA2-512 \
  23. RSA-SHA3-256 RSA-SHA2-256 \
  24. RSA-SHA3-384 RSA-SHA2-384 \
  25. RSA-SHA3-512 RSA-SHA2-512 \
  26. ECDSA-SHA2-224 RSA-PSS-SHA2-224 RSA-SHA2-224 \
  27. ECDSA-SHA1 RSA-PSS-SHA1 RSA-SHA1
  28. tls_cipher = AES-256-GCM AES-256-CCM CHACHA20-POLY1305 AES-256-CBC \
  29. AES-128-GCM AES-128-CCM AES-128-CBC
  30. cipher = AES-256-GCM AES-256-CCM CHACHA20-POLY1305 CAMELLIA-256-GCM \
  31. AES-256-CTR AES-256-CBC CAMELLIA-256-CBC AES-128-GCM AES-128-CCM \
  32. CAMELLIA-128-GCM AES-128-CTR AES-128-CBC CAMELLIA-128-CBC
  33. # 'RSA' is intentionally before DHE ciphersuites, as the DHE ciphersuites have
  34. # interoperability issues in TLS.
  35. key_exchange = ECDHE RSA DHE DHE-RSA PSK DHE-PSK ECDHE-PSK ECDHE-GSS DHE-GSS
  36. protocol = TLS1.3 TLS1.2 DTLS1.2
  37. ike_protocol = IKEv2
  38. min_tls_version = TLS1.2
  39. min_dtls_version = DTLS1.2
  40. # Parameter sizes
  41. mi

支持TLS1.2,也支持TLS1.3.

请求该服务器的网址显示的是TLS1.3,但另一台仅支持TLS1.2的服务器发来的请求依然可以成功创建连接并通信。

②经查看服务器B(Windows server 2012 r2) ssl密码件如下:

  1. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256_P256
  2. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256_P384
  3. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256_P521
  4. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384_P384
  5. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384_P521
  6. TLS_RSA_WITH_AES_128_CBC_SHA256
  7. TLS_RSA_WITH_AES_256_CBC_SHA256
  8. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256
  9. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P384
  10. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P521
  11. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256
  12. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384
  13. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P521
  14. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256_P256
  15. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256_P384
  16. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256_P521
  17. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384_P384
  18. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384_P521
  19. TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
  20. TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
  21. TLS_RSA_WITH_NULL_SHA
  22. TLS_RSA_WITH_AES_128_CBC_SHA
  23. TLS_DHE_DSS_WITH_AES_128_CBC_SHA

2.solution

这个错误有些人说是请求TLS1.3的服务必须是由支持TLS1.3的客户端才能成功建立连接是不正确的。详细要看TLS1.3的服务所在的服务器是否支持TLS1.2,如果支持,只需要保证两台服务器密码套件兼容就可以成功建立连接。

我解决此错误的办法:

将服务器B密码套件修改为默认后,再次通信即解决问题。

Final、Supplementary htpps knowledge

参考链接

1、关于HTTP、HTTPS、TLS的关系:HTTPS连接是由HTTP协议与TLS协议共同完成。

2、建立HTTPS连接不仅需要Client与Server双方的TLS协议版本号兼容,还需要Cipher Suites(密码套件)兼容。关于什么是Cipher Suites可以自行查阅资料,本文不详细展开说明。Cipher Suites的样子如图所示:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/448835
推荐阅读
相关标签
  

闽ICP备14008679号