简体   繁体   English

ASP.net核心信号器角度客户端,错误“对预检请求的响应未通过访问控制检查”

[英]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.

相关问题 ASP.NET Core Web API + Angular 对预检请求的响应未通过访问控制检查:预检请求不允许重定向 - ASP.NET Core Web API + Angular Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request 在 ASP.NET 中,对预检请求的响应未通过访问控制检查:No 'Access-Control-Allow-Origin' 标头 - In ASP.NET, Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header Angular 6 - 对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头 - Angular 6 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header 对预检请求的响应未通过访问控制检查(Angular2) - Response to preflight request doesn't pass access control check (Angular2) .NET 5 对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin' header 存在于请求的资源上 - .NET 5 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource 如何处理错误“对预检请求的响应未通过访问控制检查:'Access-Control-Allow-Credentials'的值” - How to handel error “Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials'” 对预检请求的响应未通过访问控制检查:否'Access-Control-Allow-Origin' - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' CORS WCF:对预检请求的响应未通过访问控制 - CORS WCF: Response to preflight request doesn't pass access control CORS:对预检请求的响应未通过访问控制检查:它没有 HTTP 正常状态 - CORS: Response to preflight request doesn't pass access control check: It does not have HTTP ok status 对预检请求的响应未通过访问控制(Angular 2-Web Api) - Response to preflight request doesn't pass access control (Angular 2 - Web Api)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM