简体   繁体   English

Azure移动服务Web Api上的SignalR CORS

[英]SignalR CORS on Azure Mobile Services Web Api

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 into the mix. 我有一个运行Web Api和c#的Azure移动服务,并按照Azure移动Serivce .NET后端上的启用CORS中的建议启用了CORS,但是现在我来将SignalR添加到组合中。

SignalR is working fine however I can't see to find how to enable CORS. SignalR工作正常,但是我看不到如何启用CORS。

At present in my test app config I have the following: 目前,在我的测试应用配置中,我有以下内容:

//enable CORS for WebAPI
var cors = new EnableCorsAttribute("*", "*", "*");
httpconfig.EnableCors(cors);
//rather than use the static method new up SignalRExtensionConfig and pass the current config, hopefully allowing CORS...
var signalRConfig = new SignalRExtensionConfig();
signalRConfig.Initialize(httpconfig, ioc);

But CORS doesn't work for SignalR hubs, it only works for WebAPI :( I get the frustrating: 但是CORS不适用于SignalR集线器,它仅适用于WebAPI :(我很沮丧:

No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'null' is therefore not allowed access. 因此,不允许访问原始“空”。

I have inspected the response headers and can confirm nothing is being sent back. 我已经检查了响应头,并可以确认没有任何内容被发回。

Can anyone advise? 有人可以建议吗?

I'm using the code below to add CORS to SignalR in my WebAPI project. 我正在使用下面的代码在我的WebAPI项目中将CORS添加到SignalR。 But it's not running inside Mobile Service. 但是它不在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. 如果无济于事,请忽略。

I have found a way to enable CORS for SignalR but it doesn't seem the "Correct" way. 我找到了一种为SignalR启用CORS的方法,但似乎不是“正确”的方法。 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. Azure移动服务SignalR NuGet程序包包含OwinAppBuilderExtension类。 This class is used during startup to extend the Owin setup for signalr. 此类在启动期间用于扩展Signalr的Owin设置。 I then subclassed this and overrode the ConfigureSignalR method. 然后,我将此子类化并覆盖ConfigureSignalR方法。

Inside this method you get access to IAppBuilder. 在此方法内,您可以访问IAppBuilder。 Once here I simply added appBuilder.UseCors(CorsOptions.AllowAll); 在这里,我仅添加了appBuilder.UseCors(CorsOptions.AllowAll); before base.ConfigureSignalR(appBuilder); 在base.ConfigureSignalR(appBuilder)之前;

Now this is far from ideal as I have enabled CORS for everything and said to allow all. 现在,这远非理想,因为我为所有功能启用了CORS,并说允许所有功能。 But for dev testing this is OK and I will provide a custom CORS Policy later any how. 但是对于开发人员测试而言,这是可以的,稍后我将以任何方式提供自定义CORS政策。

The final step is to set our new subcclass(CORSSignalROwinAppBuilderExtension) to be used by our service. 最后一步是设置我们的服务要使用的新子类(CORSSignalROwinAppBuilderExtension)。

Within your HttpConfig setup 在您的HttpConfig设置中

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

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM