Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure移动服务Web Api上的SignalR CORS_C#_Asp.net_Azure_Signalr_Azure Mobile Services - Fatal编程技术网

C# Azure移动服务Web Api上的SignalR CORS

C# Azure移动服务Web Api上的SignalR CORS,c#,asp.net,azure,signalr,azure-mobile-services,C#,Asp.net,Azure,Signalr,Azure Mobile Services,我有一个运行Web Api和c#的Azure移动服务,并按照中的建议启用了CORS 然而,我现在来加入信号器的组合 信号器工作正常,但我看不到如何启用CORS 目前,在我的测试应用程序配置中,我有以下内容: //enable CORS for WebAPI var cors = new EnableCorsAttribute("*", "*", "*"); httpconfig.EnableCors(cors); //rather than use the static method new u

我有一个运行Web Api和c#的Azure移动服务,并按照中的建议启用了CORS 然而,我现在来加入信号器的组合

信号器工作正常,但我看不到如何启用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:(我感到沮丧:

请求的服务器上不存在“Access Control Allow Origin”标头 因此,不允许访问源“null”

我已经检查了响应标题,可以确认没有任何内容被发送回


有人能提供建议吗?

我正在使用下面的代码将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); }); } } 公营创业 { 公共无效配置(IAppBuilder应用程序) { app.Map(“/signal”,Map=> { //将CORS中间件设置为在Signal之前运行。 //默认情况下,这将允许所有来源。您可以 //通过以下方式配置源和/或http谓词集: //提供具有不同策略的cors选项。 地图。UseCors(CorsOptions.AllowAll); var hubConfiguration=新的hubConfiguration { //您可以通过取消注释下面的行来启用JSONP。 //JSONP请求是不安全的,但是一些较旧的浏览器(以及 //IE版本)要求JSONP跨域工作 //EnableJSONP=true EnableJavaScriptProxies=false }; //运行信号器管道。我们没有使用MapSignal //因为此分支已经在“/signal”路径下运行。 运行信号器(HUB配置); }); } }
粘贴作为答案,因为它在评论中没有多行代码。如果没有帮助,请忽略。

我找到了一种方法来为SignalR启用CORS,但它似乎不是“正确”的方法。但是,在收到我们的移动服务朋友的回复之前,它足以用于开发

Azure Mobile Services SignalR NuGet包包含一个OwinAppBuilderExtension类。该类在启动期间用于扩展SignalR的Owin设置。然后我将其子类化并重写ConfigureSignalR方法

在这个方法中,您可以访问IAppBuilder

现在这还远远不够理想,因为我已经为所有东西启用了CORS,并说允许所有东西。但是对于开发人员测试来说,这是可以的,我将在以后提供一个定制的CORS策略

最后一步是设置服务要使用的新子类(CORSSignalROwinAppBuilderExtension)

在HttpConfig设置中

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

希望这能有所帮助

不幸的是,你不能用Azure移动服务运行自定义的启动类,虽然我在其他一些网站上也使用过,但这是朝着正确方向迈出的一步