【问题标题】:Web Application Cors .NET CORE 3.1 vs 2.1Web 应用程序 Cors .NET CORE 3.1 与 2.1
【发布时间】:2020-04-27 04:31:45
【问题描述】:

最近我一直在慢慢切换到 .NET CORE 3.1,但当我尝试移植一些 Web 应用程序(Restful API)时,我遇到了 Cors 的问题。

运行 .NET CORE 2.1 的项目代码:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAnyOrigin",
                builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });

            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("AllowAnyOrigin");

            app.UseHttpsRedirection();

            app.UseMvc();
        }

运行 .NET CORE 3.1 的项目代码:

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddCors(options =>
            {
                options.AddPolicy("AllowAnyOrigin",
                builder => builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader());
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }

        // 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();
            }
            else
            {
                app.UseHsts();
            }

            app.UseCors("AllowAnyOrigin");

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

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

当我向运行 2.1 的 API 发出请求时,一切都按预期进行,但如果我尝试向运行 3.1 的 API 发出相同的请求,则会收到 405 错误(方法不允许错误)。

有没有其他人遇到过这个问题,如果有,解决办法是什么?

【问题讨论】:

    标签: c# api web .net-core


    【解决方案1】:

    在你的配置方法中,移动:

    app.UseCors("AllowAnyOrigin");
    

    在之间

    app.UseRouting();
    

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

    文档中有一条警告指出,必须在这两种方法之间配置 cors 中间件,否则中间件将停止运行。

    https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-3.1

    请参阅标有“将 CORS 策略应用于所有端点”的部分

    在管道中配置组件既很棒,因为它可以让您控制,但由于订购限制而带来巨大的痛苦,我个人正在等待有人创建一个 VS 插件来分析这些并抛出警告。如果有人愿意,那将是一个很棒的项目。

    【讨论】:

    • 非常感谢,您不会相信我为此苦苦挣扎了多久。我只是不明白为什么这是一个要求,因为你不需要在 2.1 中担心它。
    • @AlexanderBruun 去过那里,所以我绝对可以相信!
    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 2021-04-11
    • 1970-01-01
    • 2020-06-14
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多