How to use cross-domain connections (CORS - Access Control Allow Origin) with SignalR

codezoo picture codezoo · Apr 3, 2012 · Viewed 39k times · Source

Using persistent connections and an ASP.NET JavaScript client, I am trying to connect to a sub-domain not the same as the one the page was served from.

ASP.Net Page from webserver sub1.mydomain.com wants to connect to SignalR at sub2.mydomain.com. The same code works fine when connecting within the same sub-domain.

I found another post where cross-domain connections were enabled with:

jQuery.support.cors = true;

but this did not work for me.

How can I connect to SignalR in a second sub-domain using persistent connection and a JavaScript client?

Answer

Piotr Szmyd picture Piotr Szmyd · Apr 3, 2012

You need to do one of the following to make it work:

  • Set up $.connection.hub.url = 'http://subdomain.domain.com/signalr';, pointing to your subdomain.
  • Enable cross domain on the server:

    RouteTable.Routes.MapHubs(new HubConfiguration()
    {
      EnableCrossDomain = true
    });