简体   繁体   English

根据官方文档,Asp.net Core 3.0 CORS 不工作

[英]Asp.net Core 3.0 CORS not working based on official documentation

I want to enable CORS with Asp.Net Core 3.0 API project.我想用 Asp.Net Core 3.0 API 项目启用 CORS。 This is the basic generated Asp.Net Core Api template.这是基本生成的 Asp.Net Core Api 模板。 Everything is default from the template, except I added CORS settings from the documentation: Enable Cross-Origin Requests (CORS) in ASP.NET Core模板中的所有内容都是默认设置,除了我从文档中添加了 CORS 设置: Enable Cross-Origin Requests (CORS) in ASP.NET Core

Here it is my Startup.cs这是我的 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();
            });
        }
  }

And these are my response headers:这些是我的响应标头: 在此处输入图像描述

What should I set up for getting corret CORS headers in Response?我应该如何设置才能在响应中获取正确的 CORS 标头?

Here it is my fetch api test:这是我的 fetch api 测试: 在此处输入图像描述

Oops, I successed to solve this.糟糕,我成功解决了这个问题。

This combination was the only one to me, what is working:这种组合对我来说是唯一的组合,什么是有效的:

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();
            });
        }
}

If you put the API URL directly in the browser, CORS is not involved.如果将API URL直接放在浏览器中,则不涉及CORS。 It isn't a cross-origin request.这不是跨域请求。

That only happens when you have a page hosted at one origin, eg localhost:5000, which has JavaScript code that calls an API at origin localhost:5001.这只发生在你有一个页面托管在一个来源时,例如 localhost:5000,它的 JavaScript 代码在来源 localhost:5001 调用 API 代码。 (or otherwise fetches resources from another origin) (或以其他方式从另一个来源获取资源)

But in your case the only origin is the API origin and thus CORS is not needed.但在您的情况下,唯一的来源是 API 来源,因此不需要 CORS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM