Asp.net core doesn't respond to preflight requ

2019-07-15 06:26发布

I've added the following code in the file startup.cs.

In method: ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddApplicationInsightsTelemetry(Configuration);

    var connection = Configuration.GetConnectionString("DefaultConnection");
    services.AddDbContext<Models.StaticDataContext>(options => options.UseSqlServer(connection));

    services.AddMvc(o => {});
    services.AddOptions();
    services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
    services.AddCors(o=> { o.AddPolicy("AllowSpecificOrigin", b => b.WithOrigins("*")); });
    // Tried it without any parameter
    // services.AddCors(); 
}

In method: Configure:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();
    loggerFactory.AddNLog();

    env.ConfigureNLog("nlog.config");

    app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader());

    app.UseApplicationInsightsRequestTelemetry();
    app.UseApplicationInsightsExceptionTelemetry();

    app.UseMvc();
}

However it doesn't return the headers of Access-Control-*?

OPTIONS http://localhost:5001/api/Deal2 HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

The raw response got from fiddler.

HTTP/1.1 204 No Content
Date: Sun, 27 Nov 2016 23:32:31 GMT
Server: Kestrel

And there should be some headers of the following?

Access-Control-Allow-Origin: YOUR_DOMAIN
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: YOUR_CUSTOM_HEADERS

1条回答
beautiful°
2楼-- · 2019-07-15 06:45

Not sure if this is your problem, but it might help.

IIS needs to handle the preflight OPTION requests for ASP.NET Core. There is more information on this post.

My answer at the end and shows how to enable IIS to handle these requests.

查看更多
登录 后发表回答