簡體   English   中英

ASP.net核心信號器角度客戶端,錯誤“對預檢請求的響應未通過訪問控制檢查”

[英]ASP.net core signalr angular client, error “Response to preflight request doesn't pass access control check”

所以我在ConfigureServices方法的ASP.Net核心服務器startup.cs中有以下代碼:

services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
            builder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .WithOrigins("https://localhost:44345");
        }));

並在Configure方法中:

app.UseCors("CorsPolicy");

在角度我有這個:

  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 :('));

}

客戶端部分和服務器部分將在不同的域上運行,它們是Visual Studio中的2個獨立項目。

無論如何,當運行角度代碼我得到這個錯誤:

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.

因此,我想我有2個選項,要么使客戶端代碼為withCredentials屬性傳遞false,要么以某種方式更改服務器端代碼,以發送將Access-Control-Allow-Credentials響應標頭設置為true。 問題是我不知道該怎么做,在任何地方都找不到withCredentials的設置。

我已經解決了,我需要在services.addCors部分中包含.AllowCredentials()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM