Azure 移动服务 Web Api 上的 SignalR CORS

2023-12-03

我有一个运行 Web Api 和 c# 的 Azure 移动服务,并按照中的建议启用了 CORS在 Azure 移动服务 .NET 后端启用 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”标头 资源。因此,不允许访问来源“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);
                });
        }
    }
  

粘贴作为答案,因为它在注释中没有多行代码。如果没有帮助请忽略。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Azure 移动服务 Web Api 上的 SignalR CORS 的相关文章

随机推荐