Asp.net核心没有使用适当的Access-Control- *标题来响应预检请求(OPTION)?

时间:2016-11-28 04:22:25

标签: asp.net .net asp.net-core .net-core

我在文件startup.cs中添加了以下代码。

方法: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(); 
}

方法:配置:

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();
}

但是它不会返回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

原始反应来自小提琴手。

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

以下应该有一些标题?

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

1 个答案:

答案 0 :(得分:0)

不确定这是否是您的问题,但可能有所帮助。

IIS需要处理ASP.NET Core的预检OPTION请求。有关此post的更多信息。

我的答案最后说明了如何启用IIS来处理这些请求。