Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

I have a Blazor server app in .Net 5 and it works fine. Newly I have added a SignalR HubConnection for notify messages, but I face with an exception due to SSL connection establishment in production sever as follows:

Workflow2.Common.DalException: The SSL connection could not be established, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch
   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
[2022-01-26 13:07:30.970][ERR]: InnerException Message
System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch
   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)

The application works without any issues if it bounded with http and I can receive notifications correctly, the issue happens only for https binding.

Here is my code for starting HubConnection:

try
{
    string sHubUrl = NavManager.BaseUri;
    sHubUrl = sHubUrl.TrimEnd('/') + "/call";

    hubConnection = new HubConnectionBuilder()
        .WithUrl(sHubUrl, options => {
            options.UseDefaultCredentials = true;
        })
        .WithAutomaticReconnect()
        .Build();
    hubConnection.On<string, string>("NewMessage", ReceivedNotification);

    await hubConnection.StartAsync();
}
catch (Exception ex)
{
    LogBuilder.LogExecption(new DalException(ex));
}

I tryed to test it with both self-signed certificate and a valid commercial certificate, but in both cases I face with the same result.

Are there any idea, what can I do to solve my Problem?

Now after some days, I set up VS remote debugging tools and with a break at occured exception, I saw that exactly the same error has happen:

The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch

   at System.Net.Security.SslStream.SendAuthResetSignal(ProtocolToken message, ExceptionDispatchInfo exception)
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Boolean async, Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
like image 847
ashokoienia Avatar asked Sep 15 '25 13:09

ashokoienia


1 Answers

I could resolve my problem with bypassing certirficate:

    hubConnection = new HubConnectionBuilder()
        .WithUrl(sHubUrl, options => {
            options.UseDefaultCredentials = true;
            options.HttpMessageHandlerFactory = (msg) =>
            {
                if (msg is HttpClientHandler clientHandler)
                {
                    // bypass SSL certificate
                    clientHandler.ServerCertificateCustomValidationCallback +=
                        (sender, certificate, chain, sslPolicyErrors) => { return true; };
                }

                return msg;
            };
        })
        .WithAutomaticReconnect()
        .Build();

and now SignalR HubConnection starts successfully if I open the page using an internal IP address in my private network for example https://10.0.0.36/call but if I want to open the page using a public IP address such as https://example.com/call, it throws an exception with an InnerException equal to null and StartAsync fails.

like image 126
ashokoienia Avatar answered Sep 17 '25 19:09

ashokoienia