Azure移动服务Web API上的SignalR CORS
-
21-12-2019 - |
题
我有一个天蓝色的移动服务运行web api和c#,并在启用Azure Mobile Serivce .NET后退 但是我现在已经进入了混音中的信号。
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不适用于信号枢纽,它只适用于webapi :(我令人沮丧:
未请求的“访问控制 - 允许原始”标题 资源。因此,不允许访问“NULL”。
我已经检查了响应标题,可以确认没有发送回来。
任何人都可以建议?
解决方案 2
我找到了一种方法来使CORS成为信号态,但它看起来并没有“正确”的方式。但是在从我们的移动服务朋友收到消息之前,它足以进行发展,直到我听到的朋友。
Azure Mobile Services信号R Nuget Package包含OwinAppBuildeRexting类。在启动期间使用此类以扩展SignalR的OWIN设置。然后,我将其子类化并overrode configuresignalr方法。
在此方法内,您可以访问IAPPBuilder。在这里,我简单地添加了appbuilder.usecors(corsoptions.allowall);base.configuresignalr(appbuilder)之前;
现在,这远非理想,因为我为所有东西都有了CORS,并且说允许所有人。但对于DEV测试这是可以的,我会在稍后提供自定义CORS策略。
最后一步是设置我们的服务使用的新子类(CORSSIGNALROWINAPPBUIDERENCED)。
在httpconfig设置中
var configBuilder = new ConfigBuilder(options, (httpconfig, ioc) =>
{
ioc.RegisterInstance(new CORSSignalROwinAppBuilderExtension(httpconfig)).As<IOwinAppBuilderExtension>();
});
.
希望这有助于
其他提示
我正在使用下面的代码将CORS添加到WebAPI项目中的SignalR。但它没有在移动服务内运行。不确定是否有帮助。
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); }); } }.
粘贴为答案,因为它在注释中没有多行代码。如果它没有帮助,请忽略。