【问题标题】:Why DELETE request blocked by CORS policy on ASP.NET Core 3.1 after project published to IIS?为什么在项目发布到 IIS 后被 ASP.NET Core 3.1 上的 CORS 策略阻止的 DELETE 请求?
【发布时间】:2021-12-31 19:06:18
【问题描述】:

我有一个用于 fornt-end 的 Angular 9 项目。实际上它正在工作,但后来我在我的角度项目“ngx-saveAs”和“saveAS”中添加了一些 NPM 包,但我不确定这是一个相关的原因。有什么想法可以帮助我找到它吗? 这是我的启动:

 services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.WithOrigins("http://localhost:4200",
                                    "https://localhost:4200",
                                    "http://192.168.10.82",
                                    "http://192.168.10.82:4200",
                                    "https://192.168.10.82",
                                    "https://192.168.10.82:4200",
                                    "192.168.10.82",
                                    "192.168.10.82:4200",
                                    "192.168.10.164",
                                    "192.168.10.163",
                                    "http://192.168.10.163:64534",
                                    "http://192.168.10.163:64535",
                                    "http://192.168.10.164",
                                    "http://192.168.10.164/",
                                    "https://192.168.10.164",
                                    "http://192.168.10.164:4200",
                                    "http://192.168.10.164:4200/",
                                    "https://192.168.10.164:4200",
                                    "http://192.168.10.163:64534/",
                                    "http://192.168.10.163:64535/"
                    ).WithMethods("GET", "POST", "DELETE", "PUT")
                    .AllowAnyMethod().WithExposedHeaders("X-Pagination")
                    .AllowAnyHeader().AllowCredentials();
             
            }));

并且错误是: Access to XMLHttpRequest at 'http://192.168.10.163:64535/api/v1/Formula/24' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

和:DELETE http://192.168.10.163:64535/api/v1/Formula/24 net::ERR_FAILED

和:

ERROR 
Error: خطا در برقراری ارتباط با سرور at ApiErrorHandlerService.handleError (http://localhost:4200/main.js:10307:27) at CatchSubscriber.selector (http://localhost:4200/main.js:10400:177) at CatchSubscriber.error (http://localhost:4200/vendor.js:285330:31) at FilterSubscriber._error (http://localhost:4200/vendor.js:282590:26) at FilterSubscriber.error (http://localhost:4200/vendor.js:282570:18) at MergeMapSubscriber.notifyError 

【问题讨论】:

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


    【解决方案1】:

    问题

    您的 CORS 配置存在两个主要问题:

    1. 您拨打的是WithMethods,然后是AllowAnyMethod。从the aspnetcore source code 可以明显看出,后者取消了前者并导致Access-Control-Allow-Methods: * 标头响应预检请求。但是,the asterisk (*) in that header only works as a wildcard for non-credentialed requests。而且您的服务器需要有凭据的请求,因为您还调用了AllowCredentials
    2. 您正在呼叫AllowAnyHeaderAccess-Control-Allow-Headers: * 标头中的 results 响应预检请求。但是,再次(与 1. 中相同)the asterisk (*) in that header only works as a wildcard for non-credentialed requests

    这一切在MDN's page on CORS上都有清楚的解释,你应该花时间仔细阅读。

    解决方案

    如果您拨打AllowCredentials,则无法拨打AllowAnyHeaderAllowAnyMethod(或AllowAnyOrigin)。您必须明确指定允许的方法和标头。像这样的:

    builder
      .WithOrigins("http://localhost:4200")
      .WithMethods("GET", "POST", "DELETE", "PUT")
      .WithHeaders() // <--- list the allowed headers here
      .AllowCredentials()
      .WithExposedHeaders("X-Pagination");
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2021-12-17
      • 2022-01-14
      • 2020-06-02
      • 2019-11-30
      • 2019-10-01
      • 2019-07-01
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多