Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-domain does not work with signalr 2.0

I have a signalr 2.0 server that should serve the multiple domains.. So in need to enable CORS in my server. I user iis7.5 as web server. I enabled the CORS in Startup method of my project as follows

 public void Configuration(IAppBuilder app)
        {
        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);
}
}

This code is copy and pasted from this article http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-javascript-client

I created a localhost project and try to connect to signalr server.

But I get the following error in firefox

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://MyWebSite.com:8082/signalr/negotiate?connectionData=%5B%7B%22name%22%3A%22sahragostarhub%22%7D%5D&clientProtocol=1.3&_=1405622027746 This can be fixed by moving the resource to the same domain or enabling CORS. negotiate

and this error in chrome XMLHttpRequest cannot load http://MyWebSite.com:8082/signalr/negotiate?connectionData=%5B%7B%22name%22%3A%22sahragostarhub%22%7D%5D&clientProtocol=1.3&_=1405622032883. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '(My Client site address)' is therefore not allowed access

I Also add the following lines to my web.config

 <httpProtocol>
      <customHeaders>
        <clear />
       <add name="Access-Control-Allow-Origin" value="*" />
      </customHeaders>
    </httpProtocol>

This change was also useless.

like image 803
Alborz Avatar asked Jul 17 '14 19:07

Alborz


People also ask

How to use SignalR with cross domain?

How to use SignalR with cross domain? 1 Create the project for signalr hub:#N#Create a standard c 2 project signalrdemo. Install SignalR via nuget. The... 3 The client website (Javascript client): More ...

How do I enable cors on the server to call cross domain?

Create a standard c# project signalrdemo. Install SignalR via nuget. The following example gives the live currency per second. We must enable CORS on the server so we can call it cross domain. Install Microsoft.Owin.Cors via nugget // Setup the CORS middleware to run before SignalR. // By default this will allow all origins.

How to enable Cors in SignalR?

We must enable CORS on the server so we can call it cross domain. Install Microsoft.Owin.Cors via nugget // Setup the CORS middleware to run before SignalR. // By default this will allow all origins. You can // providing a cors options with a different policy. // You can enable JSONP by uncommenting line below. // Run the SignalR pipeline.

Why am I getting a SignalR error when starting the connection?

This error is commonly seen if code references SignalR objects before the connection is started. The wireup for handlers and the like that will call methods defined on the server must be added after the connection completes. Note that the call to Start is asynchronous, so code after the call may be executed before it completes.


2 Answers

Try removing WebDAV module in your configuration (helped me some time ago):

<configuration>              
    <system.webServer>                   
        <modules>
            <remove name="WebDAVModule" />
...
like image 191
Ilya Luzyanin Avatar answered Sep 18 '22 00:09

Ilya Luzyanin


I experienced this today, and after poking around a bit, found that the installing the Microsoft.Owin.Cors package resolved the issue for me.

Just run the following command in the Package Manager Console:

Install-Package Microsoft.Owin.Cors

Build, and reload the page.

like image 45
Lockszmith Avatar answered Sep 21 '22 00:09

Lockszmith