【发布时间】:2022-02-03 03:34:19
【问题描述】:
在我的项目中,negotiate、connect 和 start API 调用都包含所需的 access-control-allow-origin 标头。但是,一旦 SignalR 调用 ping 方法,它就会抛出以下错误:
Access to XMLHttpRequest at 'https://backend.url/signalr/signalr/ping&_=1643292246714'
from origin 'https://frontend.url' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我已经根据微软的文档修改了我的 Startup.cs 文件。我是旧版本,我使用的是MapSignalR 方法。
public class Startup {
public void Configuration(IAppBuilder app) {
// Branch the pipeline here for requests that start with "/signalr"
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR. By default this will allow all origins.
map.UseCors(CorsOptions.AllowAll);
map.RunSignalR();
});
app.UseCors(CorsOptions.AllowAll);
}
}
但是,此更改没有任何效果。 CORS 错误仍然存在。知道这里有什么问题吗?
我将 Microsoft.AspNet.SignalR 2.4.2 和 Microsoft.Owin.Cors 4.2.0 与 .NET Framework 4.8 一起使用。
我也不明白,为什么它在 URL 中调用 /signalr/signalr。
【问题讨论】:
-
您还必须在客户端应用中配置 CORS
标签: c# asp.net-mvc cors signalr