简体   繁体   English

升级到.NET Core 2.2后的CORS问题

[英]CORS issues after upgrading to .NET Core 2.2

I had an .NET Core 2.1 application which handled CORS well. 我有一个.NET Core 2.1应用程序,可以很好地处理CORS。 But after upgrading to 2.2 I faced the problem: 但是升级到2.2后,我遇到了问题:

谷歌浏览器

Access to XMLHttpRequest at ' https://content.xxxx/user/avatar ' from origin ' https://cabinet.xxxx ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 从起源' https://cabinet.xxxx '在' https://content.xxxx/user/avatar '处对XMLHttpRequest的访问已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否'访问-Control-Allow-Origin'标头出现在请求的资源上。

Response Headers: (might this be an issue with IIS?) 响应标题:(这可能是IIS的问题吗?)

Connection: close 连接方式:关闭

Content-Length: 315 内容长度:315

Content-Type: text/html; 内容类型:text / html; charset=us-ascii 字符集= us-ascii

Date: Tue, 22 Jan 2019 15:08:26 GMT 日期:2019年1月22日星期二15:08:26 GMT

Server: Microsoft-HTTPAPI/2.0 伺服器:Microsoft-HTTPAPI / 2.0

I tried every combination I could find here (enabling CORS before and after MVC, adding and removing UseCors from Configure method), but nothing worked. 我尝试了在这里可以找到的每种组合(在MVC之前和之后启用CORS,从Configure方法中添加和删除UseCors),但是没有任何效果。

 public IServiceProvider ConfigureServices(IServiceCollection services)
    {
       ...
        services.AddCors(options =>
        {
            options.AddPolicy("MyPolicy",
                builder => builder.WithOrigins("https://cabinet.xxxxxx")
                                  .AllowAnyMethod()
                                  .AllowCredentials()
                                  .AllowAnyHeader());
        });


        services.AddMvc();
        ...
    }

   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseHsts();
        //app.UseHttpsRedirection();
        app.UseCors("MyPolicy");
        app.UseMvc();
        app.UseDefaultFiles();
        app.UseStaticFiles();
    }

and the method is 方法是

    [HttpPost]
    [EnableCors("MyPolicy")]
    [Route("user/avatar")]
    [Authorize]
    public async Task Method(IFormFile file) {...}

Am I missing something? 我想念什么吗? I don't want to downgrade back to 2.1. 我不想降级到2.1。

Apparently there was an issue with IIS configuration. 显然,IIS配置存在问题。 Moving website to another port helped. 将网站移至另一个端口有所帮助。

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

相关问题 升级到 asp.net core 2.2 后为空 href - Empty href after upgrading to asp.net core 2.2 ASP.NET-CORE 2.2 SignalR CORS - ASP.NET-CORE 2.2 SignalR CORS Angular 6 .Net core 2.2 App 中的 Cors 错误 - Cors error in Angular 6 .Net core 2.2 App 如何摆脱.net核心2.2中的CORS? - How to get rid of CORS in .net core 2.2? .net core 2.2 和开放的 CORS - .net core 2.2 and wide open CORS 如何在升级到ASP.NET Core 2.2后配置Azure日志记录 - How to configure Azure logging after upgrading to ASP.NET Core 2.2 HTTP 错误 502.5 - 升级到 ASP.NET Core 2.2 后 ANCM 进程外启动失败 - HTTP Error 502.5 - ANCM Out-Of-Process Startup Failure after upgrading to ASP.NET Core 2.2 获取ArgumentException:使用ActionResult的无效类型参数 <JsonResult> &gt;作为升级到.NET Core 2.2后的返回类型 - Getting ArgumentException: Invalid type parameter using ActionResult<JsonResult>> as a return type after upgrading to .NET Core 2.2 将 .netcore 2.2 升级到 .NET 5 后出现 LINQ 错误 - LINQ error after upgrading .netcore 2.2 to .NET 5 使用 Azure Active Directory 对 .NET Core 2.2 API 进行身份验证时出现 CORS 错误 - CORS error in authenticating .NET Core 2.2 API with Azure Active Directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM