【发布时间】:2019-02-19 08:29:42
【问题描述】:
我一定是在这里做错了,但我想不通;据我所知,这似乎是一个 CORS 问题。我需要将 Access-Control-Expose-Headers: * 公开给任何来源,但 dotnet core 2.1 并没有达到我的预期。
相关Startup.cs代码:
public void ConfigureServices(IServiceCollection services)
{
//Mapping settings to POCO and registering with container
var settings = new AppSettings.ReportStorageAccountSettings();
Configuration.Bind(nameof(AppSettings.ReportStorageAccountSettings), settings);
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials();
});
});
services.AddSingleton(settings);
services.AddApiVersioning();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseMvc();
}
此应用程序托管在 Azure 中,我在 Azure 中的 CORS 设置中添加了一个 * 条目,只是为了更好地衡量。现在,每当客户端应用程序(也托管在 Azure 中)发出发布请求时,都无法通过 JS 访问标头,并且响应中不存在 Access-Control-Expose-Headers: *。但是,当我检查网络响应和使用 Fiddler 时,我可以看到标头。我已经尝试使用 Axios 和 Jquery 来访问标题以排除 JS 的任何问题。我在这里做错了什么?
在控制器中我回复:
Response.Headers.Add("Location", $"api/someLocation");
return StatusCode(StatusCodes.Status202Accepted);
【问题讨论】:
标签: c# asp.net-core cors asp.net-core-2.1