Home > front end >  Self-hosted SignalR in Windows .Net Service blocked by CORS on same server, works on other servers
Self-hosted SignalR in Windows .Net Service blocked by CORS on same server, works on other servers

Time:04-17

I've been using a Self-Hosted SignalR Windows service accessed from multiple production servers (now in Azure) for 6 years without a problem. I created an identical server for development in Azure but when I'm accessing SignalR from a browser on the SAME SERVER, SignalR gives me the following error when using either http:6287 or https:6286:

Access to XMLHttpRequest at 'http://myserver.learn.net:6287/signalr/negotiate?clientProtocol=1.5&xxxxxxx' from origin 'http://myserver.learn.net' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

However... It WORKS when connecting from OTHER SERVERS! I'm starting the connection with no errors using:

SignalR = WebApp.Start("http://myserver.learn.net:6287/");
SignalRSSL = WebApp.Start("https://myserver.learn.net:6286/");
(also SignalR = WebApp.Start("*:628x/" for both);

In my client code, I include the following script:

<script src="http://myserver.learn.net:6287/signalr/hubs"></script>

When I enter that url (or https version) in a browser ON THE SAME OR DIFFERENT SERVER, it shows the ASP.NET SignalR JavaScript Library v2.3.0-rtm page correctly! I've turned off the firewall with no change, added Microsoft.Owin.Host.HttpListener (someone suggested). I have also entered the wildcard certificate with netsh so the SignalR service can deal with the SSL connection using:

netsh http add sslcert ipport=0.0.0.0:6286 appid={12345678-db90-4b66-8b01-88f7af2e36bf} certhash=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Edit: I've also tried changing the ipport value to the real internal IP of the server as well as the public IP but no change.

So, why can't I access SignalR from the same server?

CodePudding user response:

I found a solution in another answer here that worked. I changed:

$j.connection.hub.start().done(function () {

To:

$j.connection.hub.start({ jsonp: true, xdomain: true }).done(function () {

Which worked for both internal and external clients. xdomain:true alone didn't work but when I added jsonp:true it did. I have no real idea why, just that it's working now.

  • Related