SignalR CORS on Azure Mobile Services Web Api

后端 未结 2 1827
醉酒成梦
醉酒成梦 2021-01-24 11:41

I have an Azure Mobile Service running Web Api and c# and enabled CORS as suggested in Enable CORS on Azure Mobile Serivce .NET Backend however I have now come to add SignalR in

相关标签:
2条回答
  • 2021-01-24 12:03

    I'm using the code below to add CORS to SignalR in my WebAPI project. But it's not running inside Mobile Service. Not sure if this helps.

        public class Startup
        {
            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
                            EnableJavaScriptProxies = false
                        };
                        // Run the SignalR pipeline. We're not using MapSignalR
                        // since this branch already runs under the "/signalr" path.
                        map.RunSignalR(hubConfiguration);
                    });
            }
        }
    

    Pasted as an answer since it doesn't multi-line codes in comment. Please ignore if it doesn't help.

    0 讨论(0)
  • 2021-01-24 12:28

    I have found a way to enable CORS for SignalR but it doesn't seem the "Correct" way. But it's enough to get playing with for development until I hear back from our Mobile Services friends.

    The Azure Mobile Services SignalR NuGet package contains a OwinAppBuilderExtension class. This class is used during startup to extend the Owin setup for signalr. I then subclassed this and overrode the ConfigureSignalR method.

    Inside this method you get access to IAppBuilder. Once here I simply added appBuilder.UseCors(CorsOptions.AllowAll); before base.ConfigureSignalR(appBuilder);

    Now this is far from ideal as I have enabled CORS for everything and said to allow all. But for dev testing this is OK and I will provide a custom CORS Policy later any how.

    The final step is to set our new subcclass(CORSSignalROwinAppBuilderExtension) to be used by our service.

    Within your HttpConfig setup

     var configBuilder = new ConfigBuilder(options, (httpconfig, ioc) =>
     {
           ioc.RegisterInstance(new CORSSignalROwinAppBuilderExtension(httpconfig)).As<IOwinAppBuilderExtension>();
     });
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题