gpt4 book ai didi

c# - 对预检请求的响应未通过访问控制检查 : The value of the 'Access-Control-Allow-Credentials' header in the response is ''

转载 作者:行者123 更新时间:2023-12-03 20:29:55 25 4
gpt4 key购买 nike

我正在 angular 6 和 asp.net 核心上使用 SignalR 功能。但不断收到此错误 对预检请求的响应未通过访问控制检查:响应中“Access-Control-Allow-Credentials” header 的值为“”,当请求的凭据模式为“包含”时,该值必须为“真”。

研究了一下,发现是服务器端的CORS问题,所以修改了服务器代码。

启动.cs

public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("http://localhost:4200");
}));
services.AddSignalR();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
routes.MapHub<SignalR>("/rule");
});
}

Angular 应用
ngOnInit() {
this.callSignalR()
}

private callSignalR(): void {
// set up the signal R
let connection = new signalR.HubConnectionBuilder().withUrl(environment.baseUrl+"/rule").build();
//start the connection
connection.start().then(() => connection.invoke("updateRules", this.autheService.getCurrentuser.userId));
//Get the response from the server
connection.on("updateRules", data => {console.log(data);});
}

引用

Access-Control-Allow-Origin - Angular 5

'Access-Control-Allow-Credentials' header in the response is '' which must be 'true'

https://github.com/aspnet/SignalR/issues/2095

https://github.com/SignalR/SignalR/issues/1694

https://github.com/aspnet/SignalR/issues/2110

最佳答案

您必须为 cors-policy 提供凭据,因为 signalr 也在传递 cookie。

public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("CorsPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("http://localhost:4200");
}));
services.AddSignalR();
}

关于c# - 对预检请求的响应未通过访问控制检查 : The value of the 'Access-Control-Allow-Credentials' header in the response is '' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51569826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com