[英]How to handel error “Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials'”
[英]Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is ''
我正在使用Angle 6和asp.net核心上的SignalR功能。 但是请继续获取此错误。 对预检请求的响应未通过访问控制检查:响应中“ Access-Control-Allow-Credentials”标头的值是“,当请求的凭据模式为'时,该值必须为'true'包括'。
做了一些研究,发现这是服务器端的CORS问题。因此修改了服务器代码。
startup.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");
});
}
角度应用
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-Credentials”标头为“”,必须为“ true”
https://github.com/aspnet/SignalR/issues/2095
您必须为您的cors政策使用凭据,因为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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.