【问题标题】:CORS Policy has been blocked my subdomainCORS 政策已被阻止我的子域
【发布时间】:2019-12-19 08:33:50
【问题描述】:

我有一个相同的域,其中之一是没有前缀 www 的域。例如,

  1. https://www.example.com
  2. https://example.com

第一个域工作正常,因为它是默认域。但是当我执行 CRUD 或访问任何 api 服务时,第二个出现错误。

访问 XMLHttpRequest 在 'https://www.example.com/hubCon/negotiate' 来自原点 “https://example.com”已被 CORS 策略阻止:响应 预检请求未通过访问控制检查: 响应中的“Access-Control-Allow-Origin”标头不能是 当请求的凭据模式为“包含”时,通配符“*”。这 XMLHttpRequest 发起的请求的凭证模式是 由 withCredentials 属性控制。

我试了published by Microsoft文章中的代码,还是不行。

我使用的是 .Net Core 2.2 版本。 这是我的 Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options =>
        {
            options.AddPolicy(MyAllowSpecificOrigins,
            builder =>
            {
                builder.WithOrigins("https://example.com")
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowAnyOrigin()
                .AllowCredentials();
            });
        });

        services.AddMvc().AddJsonOptions(options =>
        {
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


        services.AddSignalR();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseCors(MyAllowSpecificOrigins);
        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });

        app.UseHttpsRedirection();
        app.UseStatusCodePages();
        app.UseStaticFiles();
        app.UseAuthentication();


        app.UseSignalR(routes =>
        {
            routes.MapHub<HubSignal>("/hubCon");
        });

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}"
                );
        });
    }

所以,我不明白为什么项目会出错。

感谢您的回答。

【问题讨论】:

    标签: asp.net-core .net-core cors asp.net-core-webapi


    【解决方案1】:

    发生这种情况是因为您同时指定了两者:

    • AllowAnyOrigin()
    • AllowCredentials();

    不支持此配置。见doc

    指定 AllowAnyOrigin 和 AllowCredentials 是不安全的配置,可能导​​致跨站点请求伪造。当应用同时配置了这两种方法时,CORS 服务会返回无效的 CORS 响应。

    您应该删除对 AllowAnyOrigin 方法的调用

    【讨论】:

      【解决方案2】:

      您不能将 AllowAnyOrigin 与 AllowCredentials 一起使用。下面是一个允许使用 CORS 的通配符域的示例。

      此代码在 ConfigureServices 中:

      services.AddCors(options =>
              {
                  options.AddPolicy("_AllowOrigin",
                      builder => builder
                          .SetIsOriginAllowedToAllowWildcardSubdomains()
                          .WithOrigins("https://localhost:44385", "https://*.azurewebsites.net", "http://*.azurewebsites.net")
                          .AllowAnyHeader()
                          .AllowAnyMethod()
                          .AllowCredentials()
                      );
              });
      

      别忘了在你的控制器中装饰你的动作:

          [EnableCors("_AllowOrigin")]
      

      【讨论】:

      • 从源 'example.com' 访问 XMLHttpRequest 在 'example.com/hubCon/negotiate' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否 'Access-Control-Allow -Origin' 标头存在于请求的资源上。
      • 谢谢,我解决了这个问题。你能为这个问题投票吗?有人给了我负分。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 2018-03-22
      • 2023-04-10
      • 2020-03-07
      • 2020-07-29
      • 2021-09-17
      • 2021-06-13
      相关资源
      最近更新 更多