【发布时间】:2019-07-16 08:17:03
【问题描述】:
我的 ASP.NET 核心 API 和 Angular 客户端有问题。我想实现 SignalR 以在 API 和 Angular 之间建立连接。 cors 策略已经在我们的客户端和 API 上激活,因为我们已经可以使用我们的客户端从 API 检索数据。但现在的问题是,当我尝试使用 SignalR 时,我收到了 CORS POLICY 错误:
访问 XMLHttpRequest 在 'http://localhost:50501/CoordinatorHub/negotiate' 来自原点 'http://localhost:4200' 已被 CORS 策略阻止:响应 预检请求未通过访问控制检查:否 请求中存在“Access-Control-Allow-Origin”标头 资源。
但我们 API 的 Startup.cs 中已经有 cors 政策,就像这样:
在 ConfigureServices 方法中:
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
builder.WithOrigins("http://localhost:4200/")
.AllowCredentials()
//.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowedToAllowWildcardSubdomains());
});
在 Configure 方法内部:
app.UseCors("AllowSpecificOrigin");
在我们的客户端中,我们只想尝试在 API 和客户端之间建立连接,就像这样:
this.hubConnection.start({withCredentials: false}).then(() =>
this.hubConnection.invoke('send', 'Hello'));
【问题讨论】:
-
我遇到了同样的问题,但我无法解决。
-
同样的问题,你解决了吗?我找到的最好的帮助是这个,这是您在 ConfigureServices 中已经拥有的:github.com/aspnet/SignalR/issues/2095
-
能否请您发布您的
Configure方法
标签: angular asp.net-core cors signalr angular7