【问题标题】:Request is blocked by CORS policy请求被 CORS 策略阻止
【发布时间】:2022-01-14 18:54:07
【问题描述】:

我在本地运行一个 C# 控制器,它服务于端点 /api。我有一个带有 Javascript 的本地 html 页面,试图从该端点获取数据。但我看到控制台出现此故障。

从源“null”访问“https://localhost:44388/api”获取 已被 CORS 策略阻止:没有“访问控制允许来源” 请求的资源上存在标头。如果一个不透明的响应 满足您的需求,将请求的模式设置为“no-cors”以获取 禁用 CORS 的资源。

在我的 C# 控制器项目中,在 Startup.cs 中,我已经在 public void ConfigureServices(IServiceCollection services) 中进行了设置

    services.AddCors(options =>
    {
        options.AddDefaultPolicy(
            builder =>
            {
                builder.AllowAnyOrigin()
                                    .AllowAnyHeader()
                                    .AllowAnyMethod();
            });
    }); 

public void Configure(IApplicationBuilder app, IWebHostEnvironment env),我放了

    app.UseCors(builder =>
    {
        builder
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
    });

你能告诉我我错过了什么吗?

谢谢。

更新: 我尝试更新代码来做到这一点。但我仍然遇到同样的问题。

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

        services.AddControllersWithViews();

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });


    }

    // 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.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });

        app.UseCors("AllowAnyOrigin");
    }

【问题讨论】:

    标签: c# cors


    【解决方案1】:

    尝试使用这种语法

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddCors(o => o.AddPolicy("AllowAnyOrigin",
                          builder =>
                           {
                           builder.AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader();
                  }));
    
            .....
                
            }
    
            
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                .....
    
               // app.UseRouting();
    
                app.UseCors("AllowAnyOrigin");
    
             //  app.UseAuthorization();
             // app.UseEndpoints(..
     
             }
    

    确保 UseCors 应该在 Configure 方法的末尾,但在 UseAuthorizaton 之前。 AddCors 应该位于配置服务的顶部。

    【讨论】:

    • 谢谢@Serge。我尝试了你的建议,但我仍然遇到同样的错误。我已经在我的问题中更新了我的代码。
    • @NJohnson 你必须移动 app.UseCors("AllowAnyOrigin");在 UserRouting 之后立即放置
    • 谢谢@Serge。你帮我解决我的问题。
    猜你喜欢
    • 2019-11-30
    • 2019-10-01
    • 2021-02-28
    • 2021-05-19
    • 2021-10-25
    • 2021-12-17
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    相关资源
    最近更新 更多