3

I'm using ASP.NET Core 2.2, in which I'm consuming secure API (e.g.https://google.com) from non-secure portal. But I am getting below error while requesting API :

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.

I have already tried with ServerCertificateCustomValidationCallback , ServerCertificateCustomValidationCallback, ServicePointManager.SecurityProtocol etc. but it didn't work.

When i am using ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;, then I'm getting error:

System.NotSupportedException: The requested security protocol is not supported"

Sample code(s) which i have already tried ::

1.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls   |
                                       SecurityProtocolType.Tls11 |
                                       SecurityProtocolType.Tls12 | 
                                       SecurityProtocolType.Ssl3;

2.

using (var httpClientHandler = new HttpClientHandler())
{
    httpClientHandler.ServerCertificateCustomValidationCallback =
        (message, cert, chain, errors) => { return true; };
    using (var client = new HttpClient(httpClientHandler))
    { // somecode }
}

3.

var handler = new HttpClientHandler
{
    ServerCertificateCustomValidationCallback =
        (httpRequestMessage, cert, cetChain, policyErrors) =>
        {
            return true;
        },
        SslProtocols = SslProtocols.Tls12   |
                       SslProtocols.Tls11   |
                       SslProtocols.Tls     |
                       SslProtocols.Ssl3    |
                       SslProtocols.Ssl2    |
                       SslProtocols.Default |
                       SslProtocols.None
};
stasiaks
  • 1,268
  • 2
  • 14
  • 31
Pooja K Bhatt
  • 979
  • 2
  • 10
  • 18
  • The message of exception asked you to look into InnerException. Have you tried that? What inside? (Suggest that root cause inside) – stukselbax Aug 07 '19 at 19:22
  • The error is regarding SSL. – Pooja K Bhatt Aug 08 '19 at 06:40
  • Error is :: System.IO.IOException: The handshake failed due to an unexpected packet format. at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.PartialFrameCallback(AsyncProtocolRequest asyncRequest) --- End of stack trace from previous location where exception was thrown --- at System.Net.Security.SslState.ThrowIfExceptional() at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult) – Pooja K Bhatt Aug 08 '19 at 06:40
  • And the solution :: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; is working in windows but not in ubuntu – Pooja K Bhatt Aug 08 '19 at 06:41
  • Have you ever managed to fix this? I have the same problem. I have tried everything I could found and nothing worked. – Mihael Keehl Sep 16 '19 at 19:51
  • I have deployed my website with SSL then its working properly. – Pooja K Bhatt Sep 26 '19 at 06:05

0 Answers0