![](/img/trans.png)
[英]Conflicting System.Web.Http.Cors assembly in azure mobile services
[英]SignalR CORS on Azure Mobile Services Web Api
我有一个运行Web Api和c#的Azure移动服务,并按照Azure移动Serivce .NET后端上的启用CORS中的建议启用了CORS,但是现在我来将SignalR添加到组合中。
SignalR工作正常,但是我看不到如何启用CORS。
目前,在我的测试应用配置中,我有以下内容:
//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);
但是CORS不适用于SignalR集线器,它仅适用于WebAPI :(我很沮丧:
所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空”。
我已经检查了响应头,并可以确认没有任何内容被发回。
有人可以建议吗?
我正在使用下面的代码在我的WebAPI项目中将CORS添加到SignalR。 但是它不在Mobile Service内部运行。 不确定是否有帮助。
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); }); } }
粘贴为答案,因为它不会在注释中使用多行代码。 如果无济于事,请忽略。
我找到了一种为SignalR启用CORS的方法,但似乎不是“正确”的方法。 但这足以推动开发工作,直到我收到我们的移动服务朋友的回音。
Azure移动服务SignalR NuGet程序包包含OwinAppBuilderExtension类。 此类在启动期间用于扩展Signalr的Owin设置。 然后,我将此子类化并覆盖ConfigureSignalR方法。
在此方法内,您可以访问IAppBuilder。 在这里,我仅添加了appBuilder.UseCors(CorsOptions.AllowAll); 在base.ConfigureSignalR(appBuilder)之前;
现在,这远非理想,因为我为所有功能启用了CORS,并说允许所有功能。 但是对于开发人员测试而言,这是可以的,稍后我将以任何方式提供自定义CORS政策。
最后一步是设置我们的服务要使用的新子类(CORSSignalROwinAppBuilderExtension)。
在您的HttpConfig设置中
var configBuilder = new ConfigBuilder(options, (httpconfig, ioc) =>
{
ioc.RegisterInstance(new CORSSignalROwinAppBuilderExtension(httpconfig)).As<IOwinAppBuilderExtension>();
});
希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.