Asp.net核心是否不使用正确的Access-Control- *标头响应预检请求(OPTION)? [英] Asp.net core doesn't respond to preflight request (OPTION) with proper headers of Access-Control-*?

查看:161
本文介绍了Asp.net核心是否不使用正确的Access-Control- *标头响应预检请求(OPTION)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've added the following code in the file 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


推荐答案

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

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

IIS需要处理ASP.NET Core的预检OPTION请求。有关此帖子

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

最后我的回答是,展示了如何使IIS处理这些请求。

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

这篇关于Asp.net核心是否不使用正确的Access-Control- *标头响应预检请求(OPTION)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆