Asp.Net Core: The SSL connection could not be established

Dung Do Tien Apr 08 2021 592

Hi There,

I have a project with Asp.Net Core 3.1. On the Login page when click Submit button I will call an API to help validate the account as below:

public async Task<ApiResult<string>> Authenticate(LoginRequest request)
{
    var json = JsonConvert.SerializeObject(request);
    var httpContent = new StringContent(json, Encoding.UTF8, "application/json");

    var client = _httpClientFactory.CreateClient();
    client.BaseAddress = new Uri(_configuration["BaseAddress"]); // = https://localhost:44387
    var response = await client.PostAsync("/api/users/authenticate", httpContent);
    if (response.IsSuccessStatusCode)
    {
        return JsonConvert.DeserializeObject<ApiSuccessResult<string>>(await response.Content.ReadAsStringAsync());
    }

    return JsonConvert.DeserializeObject<ApiErrorResult<string>>(await response.Content.ReadAsStringAsync());
}

But I got an error System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.

System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. 
---> System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'HandshakeFailure'.
---> System.ComponentModel.Win32Exception (0x80090326): The message received was unexpected or badly formatted. 
--- End of inner exception stack trace --- 
at System.Net.Security.SslStream.ForceAuthenticationAsyncTIOAdapter 
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken) 
--- End of inner exception stack trace --- 
at Jackett.Common.Indexers.BaseWebIndexer.RequestWithCookiesAndRetryAsync(String url, String cookieOverride, RequestType method, String referer, IEnumerable1 data, Dictionary2 headers, String rawbody,

Please explain to me if you know any suggestions.

Have 2 answer(s) found.
  • P

    Phuong Phan Apr 08 2021

    I not sure but maybe you have to self-trust SSL for your web API. 

    To self-trust SSL you can add code below inside constructor of the class contains method call API.

    //// IMPORT LIBS
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    //////////////
    
    ServicePointManager.ServerCertificateValidationCallback =  delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
       return true; 
    };

    This code will auto return true when validating SSL for you.

    I hope it useful for you.

  • N

    Nguyen Truong Giang Apr 08 2021

    You have to sure all sites you are using HTTPS and SSL are working fine.

    If SSL not working you need to install a certificate for it by using the command below: (Run cmd with admin)

    dotnet dev-certs https --trust

    I hope it useful.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close