我正在尝试在 ASP.NET Core API 中启用 CORS,并允许从我的客户端应用程序 (Angular 6) 传递 cookie。但是,每当我尝试通过 Angular 应用程序访问端点时,我都会收到以下错误:
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'
在我的 Startup.cs 文件中,我在 ConfigureServices 下启用了 CORS,例如:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("http://localhost:4200"));
});
并在“配置”下:
app.UseCors("CorsPolicy");
在我的 Angular 6 应用程序中,我使用如下 token 调用端点:
this.http.get<T>(url, { headers: new HttpHeaders({ 'X-XSRF-TOKEN': token}), withCredentials: true });
该错误令人困惑,因为我在 .AddCors 函数中明确设置了 .WithOrigins() 中允许的来源,但它仍然说只有一个通配符。
最佳答案
如果它托管在 Azure 上。检查 Azure AppService 中的 CORS 设置。即使在 .net core 中间件中,那里的配置也将覆盖任何其他 Cors 配置。
关于c# - 使用 .NET Core 和 Angular 6 启用 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52545698/