9

I'm trying to connect to a SignalR Core hub from my UWP application.

In a .NET Core application (2.1) it works perfectly, whereas in UWP it throws an exception when hub.StartAsync() is called.

The certificate authority is invalid or incorrect

This is my code:

hub = new HubConnectionBuilder()
    .WithUrl("http://localhost:49791/hubs/status")
    .Build();

await hub.StartAsync();

What's going on?

I set guess I have to configure something in the Package Manifest, but what?

kennyzx
  • 12,845
  • 6
  • 39
  • 83
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
  • What exception then? Usually the network related permissions must be granted, https://learn.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations – Lex Li Sep 27 '18 at 02:08
  • We all overlook the error message in the title. – kennyzx Sep 27 '18 at 05:37

3 Answers3

13

In My case, I added below capabilities from Package.appxmanifest

enter image description here

Ankit
  • 760
  • 6
  • 15
6

OK, I got into SignalR's Gitter Chat and one kind user has pointed me out the fix. Kudos to him. Here is the excerpt of the conversation.

Andrew Stanton-Nurse (@anurse): Is your application using SSL? The URL seems to be http but this error should only occur when using a self-signed SSL certificate

José Manuel Nieto @SuperJMN I'm not sure! I'm using the default ASP. NET Core template for Wep API. How can I check it? Thank you for the quick response!

Andrew Stanton-Nurse @anurse try just navigating to the URL in a browser, does it redirect you to https://localhost:...?

José Manuel Nieto @SuperJMN OK, I discovered the problem here

Snapshop

For it to work, this has to be unchecked

SuperJMN
  • 13,110
  • 16
  • 86
  • 185
4

I know this was posted some time ago. I had same issue and unchecked Enable SSL as you did. And yes it works, but if you want to still keep the SSL enable and debug against https, you can add the below code. (For Debugging only, do not push this into any production). I also enabled Private Networks in the Package.appxmanifest of UWP

    Connection = new HubConnectionBuilder().WithUrl("https://localhost:44333/clienthub", options =>
                    {
                        options.HttpMessageHandlerFactory = (handler) =>
                        {
                            if (handler is HttpClientHandler clientHandler)
                            {
                                clientHandler.ServerCertificateCustomValidationCallback = ValidateCertificate;
                            }
                            return handler;
                        };
                    }).Build();

bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            // TODO: You can do custom validation here, or just return true to always accept the certificate.
            // DO NOT use custom validation logic in a production application as it is insecure.
            return true;
        }