升级到 .NET Core 2.2 后的 CORS 问题
CORS issues after upgrading to .NET Core 2.2
我有一个 .NET Core 2.1 应用程序可以很好地处理 CORS。但是升级到2.2后我遇到了问题:
Access to XMLHttpRequest at 'https://content.xxxx/user/avatar' from origin 'https://cabinet.xxxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
响应Headers:(这可能是 IIS 的问题?)
Connection: close
Content-Length: 315
Content-Type: text/html; charset=us-ascii
Date: Tue, 22 Jan 2019 15:08:26 GMT
Server: Microsoft-HTTPAPI/2.0
我尝试了在这里可以找到的所有组合(在 MVC 之前和之后启用 CORS,在 Configure 方法中添加和删除 UseCors),但没有任何效果。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddCors(options =>
{
options.AddPolicy("MyPolicy",
builder => builder.WithOrigins("https://cabinet.xxxxxx")
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
});
services.AddMvc();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
//app.UseHttpsRedirection();
app.UseCors("MyPolicy");
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
}
方法是
[HttpPost]
[EnableCors("MyPolicy")]
[Route("user/avatar")]
[Authorize]
public async Task Method(IFormFile file) {...}
我错过了什么吗?我不想降级回 2.1。
显然 IIS 配置有问题。将网站移动到另一个端口有帮助。
我有一个 .NET Core 2.1 应用程序可以很好地处理 CORS。但是升级到2.2后我遇到了问题:
Access to XMLHttpRequest at 'https://content.xxxx/user/avatar' from origin 'https://cabinet.xxxx' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
响应Headers:(这可能是 IIS 的问题?)
Connection: close
Content-Length: 315
Content-Type: text/html; charset=us-ascii
Date: Tue, 22 Jan 2019 15:08:26 GMT
Server: Microsoft-HTTPAPI/2.0
我尝试了在这里可以找到的所有组合(在 MVC 之前和之后启用 CORS,在 Configure 方法中添加和删除 UseCors),但没有任何效果。
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
services.AddCors(options =>
{
options.AddPolicy("MyPolicy",
builder => builder.WithOrigins("https://cabinet.xxxxxx")
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
});
services.AddMvc();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHsts();
//app.UseHttpsRedirection();
app.UseCors("MyPolicy");
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
}
方法是
[HttpPost]
[EnableCors("MyPolicy")]
[Route("user/avatar")]
[Authorize]
public async Task Method(IFormFile file) {...}
我错过了什么吗?我不想降级回 2.1。
显然 IIS 配置有问题。将网站移动到另一个端口有帮助。