尽管正确 headers,但 CORS 请求被拒绝
CORS request being rejected despite correct headers
在我的 ASP.Net Core 2.2 设置中,我有一个创建 "allow all" CORS 策略的方法
public static IServiceCollection AddAllowAllCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed(_ => true);
});
});
return services;
}
在ConfigureServices
中添加:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAllowAllCors();
...
}
我在 Configure
方法中激活的:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseCors("AllowAll");
...
}
当我尝试从我的 React 应用发出 PUT 请求时,OPTIONS 请求在 Chrome 中如下所示:
但是实际的 PUT 请求失败并返回 405:
尽管 OPTIONS 响应中的 Access-Control-Allow-Origin
header 是允许的。这在 2.1 中有效,但在 2.2 中无效。确切的错误信息是:
Access to fetch at 'MY_REQUEST_URI' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我也曾尝试删除政策中的 AllowCredentials()
,但这并没有什么不同。
public static IServiceCollection AddAllowAllCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyOrigin();
});
});
return services;
}
看起来你的 CORS 配置没问题。但是没有实际的 PUT 处理程序。您应该首先使用邮递员或 ARC 等其他客户端检查您的 API,然后再考虑 CORS。
关闭新 Endpoint Routing feature and disabling the WebDav module in IIS 的组合为我解决了这个问题。我相信由于端点路由,我的预检请求实际上是由 WebDav 模块处理的,而不是由我的应用程序处理的。
要关闭终结点路由,请在 .AddMvc()
中将标志设置为 false
:
services.AddMvc(options =>
{
...other options
options.EnableEndpointRouting = false;
})
在我的 ASP.Net Core 2.2 设置中,我有一个创建 "allow all" CORS 策略的方法
public static IServiceCollection AddAllowAllCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed(_ => true);
});
});
return services;
}
在ConfigureServices
中添加:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAllowAllCors();
...
}
我在 Configure
方法中激活的:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
...
app.UseCors("AllowAll");
...
}
当我尝试从我的 React 应用发出 PUT 请求时,OPTIONS 请求在 Chrome 中如下所示:
但是实际的 PUT 请求失败并返回 405:
尽管 OPTIONS 响应中的 Access-Control-Allow-Origin
header 是允许的。这在 2.1 中有效,但在 2.2 中无效。确切的错误信息是:
Access to fetch at 'MY_REQUEST_URI' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
我也曾尝试删除政策中的 AllowCredentials()
,但这并没有什么不同。
public static IServiceCollection AddAllowAllCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyOrigin();
});
});
return services;
}
看起来你的 CORS 配置没问题。但是没有实际的 PUT 处理程序。您应该首先使用邮递员或 ARC 等其他客户端检查您的 API,然后再考虑 CORS。
关闭新 Endpoint Routing feature and disabling the WebDav module in IIS 的组合为我解决了这个问题。我相信由于端点路由,我的预检请求实际上是由 WebDav 模块处理的,而不是由我的应用程序处理的。
要关闭终结点路由,请在 .AddMvc()
中将标志设置为 false
:
services.AddMvc(options =>
{
...other options
options.EnableEndpointRouting = false;
})