SignalR核心和跨域请求出现问题

编程入门 行业动态 更新时间:2024-10-28 21:29:15
本文介绍了SignalR核心和跨域请求出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们将SignalR核心与CORS跨域请求一起使用.客户端无法连接到服务器. 401在请求的资源上没有未授权的没有Access-Control-Allow-Origin标头.错误:无法启动连接.请注意,我们使用的是最新的Signalr内核(asp core 2.0的Alpha版本).

We are using SignalR core with CORS cross domain requests. The client is unable to connect to server. 401 Unauthorized No Access-Control-Allow-Origin' header is present on the requested resource. Error: Failed to start the connection. Note we are using the most recent signalr core (alpha version for asp core 2.0).

请注意,在同一客户端应用程序中,我能够访问CORS Web API方法.它与SignalR集线器/客户端隔离.

Please note within the same client app, i'm able to access CORS Web API methods. It's isolated to SignalR hub/client.

我们在IIS中使用Windows身份验证.匿名似乎正在起作用.

We are using Windows authentication in IIS. Anonymous seems to be working.

服务器startup.cs:

Server startup.cs:

ConfigureServices()方法

ConfigureServices() method

services.AddCors(); services.AddSignalR();

配置方法

app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials()); app.UseSignalR(routes => { routes.MapHub<TestHub>("test"); }); app.UseMvc();

javascript客户端:

javascript client:

let connection = new signalR.HubConnection(CORS_WEB_API_URL + '/test'); connection.on('send', data => { console.log(data); }); connection.start();

请告知.

推荐答案

我知道这是一篇老文章,但是答案是,使用SignalR时,您不能在CORS允许来源中使用通配符.

I know this is an old post, however, the answer is that with SignalR you cannot use wildcards in the CORS allow origin.

需要具体.对于开发,我使用以下方法:

Needs to be specific. For development I use this:

services.AddCors(options => options.AddPolicy("CorsDev", builder => { builder .AllowAnyMethod() .AllowAnyHeader() .WithOrigins(AppSettings.SPAHost, "localhost:8099") .AllowCredentials(); }));

然后在startup.cs中的配置"中

Then in Configure in startup.cs

app.UseCors("CorsDev");

app.UseCors("CorsDev");

希望这对某人有帮助.

更多推荐

SignalR核心和跨域请求出现问题

本文发布于:2023-10-11 17:56:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482415.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   SignalR

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!