【问题标题】:Core 2.1 refuses to respond with Access-Control-Expose-Headers: *Core 2.1 拒绝使用 Access-Control-Expose-Headers 响应:*
【发布时间】: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


    【解决方案1】:

    CorsPolicyBuilderAllowAnyHeader 方法配置Access-Control-Allow-Headers 响应头,该响应头仅用于preflighted requestsAccess-Control-Expose-Headers 响应头是需要的,它是使用WithExposedHeaders 配置的。

    这是一个完整的例子:

    services.AddCors(options =>
    {
        options.AddPolicy("AllowAll", builder =>
        {
            builder.AllowAnyHeader()
                   .AllowAnyMethod()
                   .AllowAnyOrigin()
                   .AllowCredentials()
                   .WithExposedHeaders("Location"); // params string[]
        });
    });
    

    【讨论】:

      【解决方案2】:

      正如柯克所说,.WithExposedHeaders() 方法是需要的。 柯克回答的另一个变体是:

      // in Startup.cs
      
      // at the end of ConfigureServices() add:
      services.AddCors();
      
      // at the top of Configure() add:
      app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().WithExposedHeaders("*"));
      

      【讨论】:

        猜你喜欢
        • 2016-05-18
        • 2016-05-18
        • 1970-01-01
        • 2015-09-12
        • 2013-06-12
        • 1970-01-01
        • 1970-01-01
        • 2015-03-24
        相关资源
        最近更新 更多