【发布时间】:2020-08-13 11:08:17
【问题描述】:
我有一个适用于所有端点的全局 CORS 策略,但我想为 signalR 集线器端点覆盖此策略。
这是我的 ConfigureServices 方法,它有一个我无法修改的全局 CORS 策略
public void ConfigureServices(IServiceCollection services)
{
// some piece of code before adding CORS
services.AddCors(o =>
{
o.AddDefaultPolicy(p =>
{
p.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// some piece of code after adding CORS
}
这里是配置方法
public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseCors();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endPoints =>
{
endPoints.MapControllers();
endPoints.MapHub<NotificationHub>("/notificationHub")
.RequireCors((builder) =>
{
builder
.WithOrigins(_configuration.GetValue<string>("Settings:ClientAppUrl"))
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
}
很明显,我已经覆盖了 /notificationHub 信号的特定端点的 CORS 策略。
在为 /notificationHub/negotiate 添加 CORS 策略之前,我在浏览器中遇到了相同的 CORS 错误
对预检请求的响应未通过访问控制检查:响应中的“Access-Control-Allow->Credentials”标头的值为“”,当请求的凭据模式为>时必须为“true”包括'。 XMLHttpRequest 发起的请求的凭证模式由 >withCredentials 属性控制。
另外请注意,如果我在全局 CORS 策略中添加 AllowCredentials() 方法,那么 signalR 可以正常工作,但我的目标是仅为 signalR 端点添加新的 CORS 策略。
我没有使用 OWIN CORS,它只是 Microsoft.AspNetCore.Cors。
【问题讨论】:
标签: c# asp.net-core cors signalr startup