SignalR 1.0.1 Cross Domain Request (CORS) with Chrome

I am trying to implement Cross-domain calls with SignalR 1.0.1 using Chrome (Ver 25.0.1364.172). Since I have a user interface on one host (localhost: 16881) and a "service" on another host (localhost: 16901).

I have everything in place, as in the topic How to use cross-domain connections (CORS - Access Control allow start) using SignalR

add jQuery.support.cors = true; before opening a connection set up $.connection.hub.url = 'http://localhost:16901/signalr';, pointing to your subdomain allow cross-domain requests on server side, by adding the following header description: <add name="Access-Control-Allow-Origin" value="http://localhost:16881" /> inside system.WebServer/httpProtocol/customHeaders section in Web.config file. 

I also have a HubConfiguration setting for my route mapping in global.asax for SignalR 1.0.1

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

Everything looks great in IE10 and FF22. However, in Chrome, this causes me an eros when SignalR tries to do a handshake.

 XMLHttpRequest cannot load http://localhost:16901/signalr/negotiate?_=1363560032589. Origin http://localhost:16881 is not allowed by Access-Control-Allow-Origin. 

I know that I can get it to work with Chrome by running it with --disable-web-security, but this does not meet my requirements. Please, help!

+7
source share
2 answers

Here is what you need to do:

  • Delete jQuery.support.cors = true
  • Delete <add name="Access-Control-Allow-Origin" value="http://localhost:16881" />

Then it should work fine.

+11
source

You do not need to use

jQuery.support.cors = true;

Instead, you can enable CORS support in your Startup class when configuring. Here are some examples:

  // Branch the pipeline here for requests that start with "/signalr" app.Map("/signalr", map => { // Setup the CORS middleware to run before SignalR. // By default this will allow all origins. You can // configure the set of origins and/or http verbs by // providing a cors options with a different policy. map.UseCors(CorsOptions.AllowAll); var hubConfiguration = new HubConfiguration { // You can enable JSONP by uncommenting line below. // JSONP requests are insecure but some older browsers (and some // versions of IE) require JSONP to work cross domain // EnableJSONP = true }; // Run the SignalR pipeline. We're not using MapSignalR // since this branch already runs under the "/signalr" // path. map.RunSignalR(hubConfiguration); }); 
0
source

All Articles