gpt4 book ai didi

angular - 使用 SIGNAL R 的 angular 7 和 ASP.NET core 2.2 的 CORS 策略问题

转载 作者:太空狗 更新时间:2023-10-29 17:33:21 25 4
gpt4 key购买 nike

我和我的 friend 在使用我们的 API 和 Angular 客户端时遇到了 CORS 问题。我们正在尝试建立链接,我们正在使用 signalR 并且客户端(Angular 7)注册自己以接收来自服务器(ASP .NET core 2.2)的消息。

在浏览器控制台中,我收到此消息:

Access to XMLHttpRequest at 'https://ourEndpoint/CoordinatorHub/negotiate' from origin 'http://localhost:4200' 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'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

服务器端

我们的startup.cs是这样的,我们已经按照微软文档做了

public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddDbContext<OneRoomContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("OneRoomContext")));
services.AddSignalR();
// Register the Swagger services
services.AddSwaggerDocument();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseCors(builder =>
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
// app.UseCors(builder =>
// builder.AllowAnyOrigin()
// .AllowAnyMethod()
// .AllowAnyHeader());
//}
//else
//{
// // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
// app.UseHsts();
//}
app.UseSignalR(route =>
{
route.MapHub<CoordinatorHub>("/CoordinatorHub");
});
app.UseHttpsRedirection();
app.UseMvc();
// Register the Swagger generator and the Swagger UI middlewares
app.UseSwagger();
app.UseSwaggerUi3();
}

这是我们的 Controller :

using Microsoft.AspNetCore.SignalR;
using oneroom_api.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace oneroom_api.SignalR
{
public class CoordinatorHub : Hub
{
public Task SendNewUser(User user)
{
return Clients.All.SendAsync("GetNewUser", user);
}
}
}

Azure 门户:允许 CORS * 来源

客户端

这就是我们的 app.component.ts 的样子 (ngOnInit)

我们正在使用@aspnet/signalr包

this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(localStorage.getItem('endpoint') + '/CoordinatorHub')
.build();

this.hubConnection.on('send', data => {
console.log(data);
});

this.hubConnection.start().then(() => this.hubConnection.invoke('send', 'Hello'));

如何禁用凭据模式或我需要在哪里提供 withCredentials 状态?

谢谢你的时间和答案

最佳答案

如错误消息所述,您需要明确指定允许的 CORS 来源。

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'.

您当然可以尝试让 SignalR 停止发出要求您的 API 发送 Access-Control-Allow-Credentials header 的请求,具体取决于您打算如何处理身份验证(cookie 或不记名) token ?)。这比简单地扩展“允许的来源”列表要复杂得多。除此之外,您真的应该避免对来源使用通配符,尤其是在生产系统上。

对于本地开发,将开发服务器的地址添加到允许的来源列表中就足够了。必须为您希望应用程序可访问的每个地址扩展该列表。

 app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());

除了代码更改之外,您还必须从 Azure 应用服务配置中删除通配符 CORS 条目。否则更改将无效,因为 CORS header 将被 Azure 覆盖。

关于angular - 使用 SIGNAL R 的 angular 7 和 ASP.NET core 2.2 的 CORS 策略问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54808822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com