locked
SignalR 2.4.0 persistent connection, blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource RRS feed

  • Question

  • User1497005810 posted

    I have two projects. One is <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="11" data-gr-id="11"><g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="15" data-gr-id="15">signalr</g></g> server. Another is webform(javascript) for <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12"><g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="14" data-gr-id="14">signalr</g></g> client. Both are used .net framework 4.5 and <g class="gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="13" data-gr-id="13">signalr</g> 2.4.0 and persistent connection.

    I have a problem. How do I add cors(<g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">cross domain</g>) in my <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">signalr</g> server?

    [assembly: OwinStartup(typeof(SignalRServer.Startup))]
    namespace SignalRServer
    {
    	public class Startup
    	{
    		public void Configuration(IAppBuilder app)
    		{			
    			app.MapSignalR<EchoConnection>("/realtime/echo", new ConnectionConfiguration()
    			{
    				EnableJSONP = true
    			});
    		}
    	}
    }



    Sunday, March 24, 2019 5:32 AM

Answers

  • User61956409 posted

    Hi akira32,

    I have two projects. One is signalr server. Another is webform(javascript) for signalr client. Both are used .net framework 4.5 and signalr 2.4.0 and persistent connection.

    I have a problem. How do I add cors(cross domain) in my signalr server?

    [This documentation](https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#calling-usecors) demonstrates how to implement cross-domain connections in SignalR 2, please refer to it.

    using Microsoft.AspNet.SignalR;
    using Microsoft.Owin.Cors;
    using Owin;
    namespace MyWebApplication
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                // 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);
                });
            }
        }
    }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 25, 2019 5:33 AM

All replies

  • User61956409 posted

    Hi akira32,

    I have two projects. One is signalr server. Another is webform(javascript) for signalr client. Both are used .net framework 4.5 and signalr 2.4.0 and persistent connection.

    I have a problem. How do I add cors(cross domain) in my signalr server?

    [This documentation](https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#calling-usecors) demonstrates how to implement cross-domain connections in SignalR 2, please refer to it.

    using Microsoft.AspNet.SignalR;
    using Microsoft.Owin.Cors;
    using Owin;
    namespace MyWebApplication
    {
        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                // 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);
                });
            }
        }
    }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 25, 2019 5:33 AM
  • User1497005810 posted

    Thank you! I had solved my problem.

    			app.Map("/realtime/echo", map =>
    			{
    				map.UseCors(CorsOptions.AllowAll);
    				var ConnectionConfiguration = new ConnectionConfiguration
    				{
    					// 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<EchoConnection>(ConnectionConfiguration);
    			});

    Monday, March 25, 2019 11:10 AM