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.
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 ...
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.
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.
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.
Try removing WebDAV module in your configuration (helped me some time ago):
<configuration>
<system.webServer>
<modules>
<remove name="WebDAVModule" />
...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With