.net-core - 使用 safari 时仅在子页面中出现 Cors 错误
问题描述
仅使用 safari 时,我的网站在子页面中遇到 CORS 错误。意思是,在主页上,没有 CORS 错误,并且 api 在包括 safari 在内的所有浏览器上返回 200 和正确的 json 数据。但是当涉及到https://example.com/blog
所有其他浏览器时,除了显示 CORS 错误的 safari 之外。
我的网站前端位于https://example.com
,而我的 api 位于https://api.example.com/api
我已尝试按照此处对此问题的答案中的建议添加斜杠: CORS request not working in Safari
该 api 使用 asp dotnet core 进行编码,并使用 nginx 反向代理托管在 Debian 10 上,我假设它传递了正确的标头(知道它在主页中工作正常)。这是我添加 CORS 支持的代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder.WithOrigins(Configuration.GetSection("Origins"))
.SetIsOriginAllowed(x=>_ = true)
.AllowCredentials()
.AllowAnyMethod()
.AllowAnyHeader()
.SetPreflightMaxAge(TimeSpan.FromMinutes(60)); //one hour preflight is ok
});
});
//rest of the code
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseForwardedHeaders();
app.UseHttpsRedirection();
app.UseResponseCompression();
app.UseRouting();
app.UseCors("AllowAll");
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers().RequireCors("AllowAll");
});
}
解决方案
推荐阅读
- python - 如何调整熊猫直方图上的刻度标签?
- flask - 在命令提示符下导入火炬时出现问题,但它直接在 VS 代码/Jupyter 中工作
- php - 如何通过键/值在 PHP 中分解这个 json_decoding 文件?
- windows - 如何验证文件夹中是否缺少文件(批处理脚本)?
- c - 带有双链表的随机快速排序
- oracle - 获取项目(错误报告 - 未知命令
- css - Flexbox:如何不让超大的子元素(沿横轴)过度拉伸嵌套的 flexbox
- azure - 如何从 Azure 数据块写入 RabbitMQ?
- python - 如何获取邮件回复exchangelib的message-id
- spring - 是否可以根据目标 URL 禁用 Spring Cloud Sleuth 标头传播?