![](/img/trans.png)
[英]Access to fetch at *** from origin *** has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
[英]SignalR Access to fetch has been blocked by CORS
我目前正在關注這個基本的信號 R 教程。 但是因為我想在現有項目中使用它,所以我將它拆分了。 Hub 位於與客戶端不同的項目中。 因此IP地址是不同的。 在 Hub 的配置中,我當然啟用了 cors:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
services.AddSignalR();
//other configuration code
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
app.UseRouting();
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin()
);
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<LiveUpdating.ChatHub>("/chatHub");
});
}
但是,當我現在嘗試連接到客戶端中的 chatHub 時(設置與上面的鏈接完全相同,只是我將連接更改為var connection = new signalR.HubConnectionBuilder().withUrl("https://localhost:44316/chatHub").build();
) 我收到這個錯誤:
Access to fetch at 'https://localhost:44316/chatHub/negotiate?negotiateVersion=1' from
origin 'https://localhost:44341' has been blocked by CORS policy: Response to preflight
request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin'
header in the response must not be the wildcard '*' when the request's credentials mode
is 'include'.
當我添加如上所示的 app.UseCors 時,這通常不應該起作用。 因為對於我所有的其他 Rest Controller,它都可以正常工作。
將 app.useCors 替換為
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true)
.AllowCredentials());
修復了錯誤
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.