-1

I use SignalR (Microsoft.AspNet.SignalR.SystemWeb v2.4.0 and Microsoft.AspNet.SignalR.Core v2.4.1.0) in an ASP.NET MVC application and when debugging, I encounter "Access to XMLHttpRequest at 'https://demo.com/signalr/negotiate?clientProtocol=2.1&connectionData=%5B%7B%22name%22%3A%22demohub%22%7D%5D&_=1569323349167' from origin 'http://localhost:20700' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." error. By looking on the web, I have concluded some configurations on Startup.cs as shown below, but I cannot use this configuration as I do not user ASP.NET Core (I use ASP.NET MVC). Any idea?

public void ConfigureServices(IServiceCollection services)
{
services.addc .AddCors(options =>
{
    options.AddPolicy(_corsPolicyName,
    builder =>
    {
        builder.WithOrigins(
                "https://localhost:5001",
                "https://localhost:4001",
                "https://localhost:44307",
                "https://localhost:44394"
            )
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials();
    });
});

services.AddSignalR();
}

Update: Here is my configuration to fix the problem:

Startup.cs:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(Demo.Web.UI.App_Start.Startup))]
namespace Demo.Web.UI.App_Start
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

web.config:

<system.webServer>

    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="http://localhost:20700"/>
            <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/>
            <add name="Access-Control-Allow-Headers" value="Content-Type"/>
        </customHeaders>
    </httpProtocol>    

</system.webServer>
  • 1
    There are no downvotes on this question, and a single close vote because you've not shown the code that is causing the error. I'd probably downvote for lack of research effort as searching for that error message brought [how to fix 'Access to XMLHttpRequest has been blocked by CORS policy' Redirect is not allowed for a preflight request only one route](https://stackoverflow.com/q/54212220/215552), but you've not included the whole error message so there's not much to go on... – Heretic Monkey Sep 20 '19 at 20:02
  • @HereticMonkey I think you have not read my question well. The issue you proposed is not related to my question. –  Sep 24 '19 at 11:20
  • I think you have not read my comment well. My point was that you did not include the entire error message, *so there's no way of knowing if the question is related*. In other words. [edit] your question to include the entire error message. – Heretic Monkey Sep 24 '19 at 12:44
  • @HereticMonkey Ok, I updated including full error message. On the other hand, I have tried to apply fixes on [Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method](https://stackoverflow.com/questions/6290053/setting-access-control-allow-origin-in-asp-net-mvc-simplest-possible-method), but most of them not worked or not a global fix. Instead of applying Controller stage, I would prefer a fix globally in `Global.asax` or `Startup.cs` (I also tried web.config fixes, but never worked). So, is there any global fix for this problem? –  Sep 24 '19 at 12:49
  • I've just re-read your post, the error is on the Clientside, CORS are denied from post `20700`. Have you tried to add this Origin? – Jonathan Larouche Sep 24 '19 at 13:01
  • I have tried to add this origin to web.config instead of "*", but the error still persists. –  Sep 24 '19 at 13:04
  • @hexadecimal Any chance to try the solution? – Jonathan Larouche Sep 26 '19 at 17:18
  • @JonathanLarouche Sorry for late reply, I have not found a chance to try this. But I will try asap and inform you. Thanks a lot for your kind help. Voted up for now and will be marked as answer after try and see fix the problem. Thanks. –  Oct 03 '19 at 21:01

1 Answers1

0

I've managed to make it work, i've published the source on on GitHub https://github.com/jonathanlarouche/stackoverflow_q58027442

Prerequisites packages are : Asp.Net Web Application with the following Nuget packages :
Microsoft.AspNet.SignalR.Core 2.4.1
Microsoft.AspNet.SignalR.JS 2.4.1
Microsoft.AspNet.SignalR.SystemWeb 2.4.0
Microsoft.Owin.Security 2.1.0
Microsoft.Owin 4.0.0

web.config <system.webServer> node.
IMPORTANT Access-Control-Allow-Credentials Is required when using Allow-Origin

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="http://localhost:20700" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Credentials" value="true"/>
      </customHeaders>
    </httpProtocol>
</system.webServer>

Startup.cs Configuration file

public class Startup
{

    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

In the Git solution, I've configured my Application to run under port 49611, then i've published the Html page to a empty website running under port 20700. SignalR requests now pass on port 49611 and CORS are working.

SignalR Client Html: (Working when running from http://localhost:20700/ or http://localhost:49611/)

<script type="text/javascript" src="/Scripts/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="/Scripts/jquery.signalR-2.4.1.min.js"></script>
<script type="text/javascript" src="http://localhost:49611/SignalR/hubs"></script>
<script type="text/javascript">
    ...
    var ChatProxy;
    function Connect() {
        ChatServerUrl = "http://localhost:49611/";
        var ChatUrl = ChatServerUrl + "signalr";
        //This will hold the connection to the signalr hub   
        SignalrConnection = $.hubConnection(ChatUrl, {
            useDefaultPath: false
        });
        ChatProxy = SignalrConnection.createHubProxy('ChatHub');
    }
    ...
</script>

Download the Repo and try it

  • Thanks for reply. I already use `public void Configuration(IAppBuilder app) { app.MapSignalR(); }` and wondered if there is a fix by using the config as I indicated in my question rather than JavaScript side. Any idea? –  Sep 20 '19 at 12:59
  • Have you tried to use it passing by `Microsoft.Owin.Hosting.WebApp.Start("{uri}/signalR");` ? – Jonathan Larouche Sep 20 '19 at 13:16
  • I installed Microsoft.Owin & Microsoft.Owin.Security and Microsoft.Owin.Hosting Version 3.1.0+ packages, but have no idea how to use `Microsoft.Owin.Hosting.WebApp.Start("{uri}/signalR");` Could you please post it on your answer? Thanks –  Sep 20 '19 at 13:55
  • Any reply please? –  Sep 20 '19 at 19:52
  • See `StartupCustom` Type in the line `Microsoft.Owin.Hosting.WebApp.Start("http://localhost:8181/signalR");` It should be the Startup class `public class StartupCustom` as defined in my example. I will try to publish the project in a Git repo early next week . – Jonathan Larouche Sep 20 '19 at 19:55
  • I had already define `[assembly: OwinStartup(typeof(UA.Intra.Web.UI.App_Start.Startup))]` in my Startup class. Is there any need to use Global.asax? –  Sep 24 '19 at 11:19
  • On the other hand, could you please post whole Satrtup with the related code part? –  Sep 24 '19 at 12:14
  • Since my code is using a prior WebApplication configuration with global.asax etc... It will be easier that you publish your code and I'll fix/comment it – Jonathan Larouche Sep 24 '19 at 13:56
  • Thanks. I added my configuration to the Update section of my question. –  Sep 24 '19 at 14:57
  • Got it to have the same Client Error for CORS when running on an external site. To fix it I need to know if you are using `Asp.Net MVC` (.Net Framework v4.x) OR `Asp.Net Core`? I see that you are using `ConfigureServices` wich is a DotNet Core Startup code but your Nuget Package are Asp.Net references.. – Jonathan Larouche Sep 25 '19 at 00:34
  • Dear Jonathan, I use ASP.NET MVC (.Net Framework v4.8) and SignalR (Microsoft.AspNet.SignalR.SystemWeb v2.4.0 and Microsoft.AspNet.SignalR.Core v2.4.1.0). Sorry, I do not use ConfigureServices, I just tried to use it but as I see it is belonging to Core, I remove that part. Any help pls? –  Sep 25 '19 at 09:06