[英]ASP.net core signalr angular client, error “Response to preflight request doesn't pass access control check”
So I have this code in ASP.Net core server startup.cs in ConfigureServices method : 所以我在ConfigureServices方法的ASP.Net核心服务器startup.cs中有以下代码:
services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
builder
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins("https://localhost:44345");
}));
And within Configure method : 并在Configure方法中:
app.UseCors("CorsPolicy");
In angular I have this : 在角度我有这个:
ngOnInit(): void
{ {
this._hubConnection = new signalR.HubConnectionBuilder().withUrl('https://localhost:44305/hub').build();
this._hubConnection
.start()
.then(() => console.log('Connection started!'))
.catch(err => console.log('Error while establishing connection :('));
} }
The client part and server part will run on different domains, they are 2 separate projects in visual Studio. 客户端部分和服务器部分将在不同的域上运行,它们是Visual Studio中的2个独立项目。
Anyway when running the angular code i get this error : 无论如何,当运行角度代码我得到这个错误:
Failed to load https://localhost:44305/hub/negotiate: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. Origin 'https://localhost:44345' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
So I guess I have 2 options, either get the client side code to pass false for the withCredentials attribute or somehow change the server side code do it sends a Access-Control-Allow-Credentials response header set to true. 因此,我想我有2个选项,要么使客户端代码为withCredentials属性传递false,要么以某种方式更改服务器端代码,以发送将Access-Control-Allow-Credentials响应标头设置为true。 The problem is I don't know how to do either of those things, I can't find a setting for withCredentials anywhere.
问题是我不知道该怎么做,在任何地方都找不到withCredentials的设置。
我已经解决了,我需要在services.addCors部分中包含.AllowCredentials()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.