【问题标题】:Asp.net Core 3.0 CORS not working based on official documentation根据官方文档,Asp.net Core 3.0 CORS 无法正常工作
【发布时间】:2020-02-26 02:30:47
【问题描述】:

我想通过 Asp.Net Core 3.0 API 项目启用 CORS。这是生成的基本 Asp.Net Core Api 模板。模板中的所有内容都是默认设置,除了我从文档中添加了 CORS 设置:Enable Cross-Origin Requests (CORS) in ASP.NET Core

这是我的 Startup.cs

public class Startup
{
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.WithOrigins("localhost", "www.google.com")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCors("CorsPolicy");
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
  }

这些是我的响应标头:

我应该如何设置才能在响应中获取正确的 CORS 标头?

这是我的 fetch api 测试:

【问题讨论】:

    标签: c# asp.net-core cors


    【解决方案1】:

    糟糕,我成功解决了这个问题。

    这个组合对我来说是唯一的,什么是有效的:

    public class Startup
    {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
                services.AddCors(); //This needs to let it default
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseCors(
                    options => options.SetIsOriginAllowed(x => _ = true).AllowAnyMethod().AllowAnyHeader().AllowCredentials()
                ); //This needs to set everything allowed
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
    }
    

    【讨论】:

    • 谢谢老哥,遇到了同样的问题!
    • 谢谢,但是origin应该是SetIsOriginAllowed(_ => true)(效果一样,但是dummy assignment没有意义)。
    【解决方案2】:

    如果将 API URL 直接放在浏览器中,则不涉及 CORS。 这不是跨域请求。

    只有当您将页面托管在一个来源时才会发生这种情况,例如localhost:5000,它的 JavaScript 代码调用源 localhost:5001 的 API。 (或以其他方式从其他来源获取资源)

    但在您的情况下,唯一的来源是 API 来源,因此不需要 CORS。

    【讨论】:

    • 我也使用 fetch api 对其进行了测试,结果相同(没有 cors 标头)。我的原始问题扩展为 fetch api screenshot。
    • 简单请求可能不会触发 CORS 预检。
    • 您的 CORS 配置无法让您的网站访问 Google。你倒过来了。 Google 需要将您的网站 添加到他们的 CORS 配置中,以便您访问。
    • @Amy 它是从源 www.google.com 到 localhost 的提取。当然,重要的是 OP 的 CORS。如果调用的是 Google API,那么它将是他们的。
    • @Amy 不,这不是谷歌方面的设置,这是我方面的设置,而且似乎 Asp.Net Core 3.0 CORS 设置不起作用:(
    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 2018-08-08
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 2021-08-03
    • 2017-04-06
    相关资源
    最近更新 更多