简体   繁体   English

未启用 ASP.NET Core 2 CORS

[英]ASP.NET Core 2 CORS not enabled

I'm developing a web application based on an Angular 6 client and ASP.NET Core WebAPI for web services.我正在开发基于 Angular 6 客户端和用于 Web 服务的 ASP.NET Core WebAPI 的 Web 应用程序。

At the moment (initial development phase) i have a simple architecture that consists of two web services, one that manages authentication and identities, the other one that holds the applicative logic (business logic, updating db, ecc.).目前(初始开发阶段)我有一个简单的架构,它由两个 Web 服务组成,一个管理身份验证和身份,另一个保存应用逻辑(业务逻辑、更新数据库等)。

I'm using JWT Bearer token for client authentication.我正在使用 JWT Bearer 令牌进行客户端身份验证。

Everything works fine with my authentication service, but when I try to call the application service I obtain this error in the Chrome browser console:我的身份验证服务一切正常,但是当我尝试调用应用程序服务时,我在 Chrome 浏览器控制台中收到此错误:

Failed to load http://localhost:59207/api/Files/Upload : No 'Access-Control-Allow-Origin' header is present on the requested resource.无法加载http://localhost:59207/api/Files/Upload :请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:4200 ' is therefore not allowed access.因此,不允许访问 Origin ' http://localhost:4200 '。 The response had HTTP status code 500.响应的 HTTP 状态代码为 500。

This error is preceded by another one:此错误之前是另一个错误:

POST http://localhost:59207/api/Files/Upload 500 (Internal Server Error) POST http://localhost:59207/api/Files/Upload 500(内部服务器错误)

Is, in some way, the second error I get related to the Internal Server Error it is preceded by?在某种程度上,我得到的第二个错误是否与它之前的内部服务器错误有关?

I test my POST call from Postman and everything works fine, no server-side errors and the data i want back from my service is returned.我测试了来自 Postman 的 POST 调用,一切正常,没有服务器端错误,并且返回了我想要从我的服务中返回的数据。

I already put in place everything I know about CORS in ASP.NET Core.我已经在 ASP.NET Core 中放置了我所知道的关于 CORS 的所有内容。

Startup class method named "ConfigureServices" contains, as first row:名为“ConfigureServices”的启动类方法包含,作为第一行:

        services.AddCors(options =>
        {
            options.AddPolicy("AllowAll",
                builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
        });

Then this is called in the "Configure" method:然后在“配置”方法中调用它:

app.UseCors("AllowAll");

Also I put the EnableCors Attribute on every controller class like this:此外,我将 EnableCors 属性放在每个控制器类上,如下所示:

[EnableCors("AllowAll")]

Anyone has an idea of how I can get out of this mess?任何人都知道我如何摆脱这个烂摊子? From what I get, this is how CORS is intended to be used and I already got it running this way on other projects (but never with ASP.NET Core 2).据我所知,这就是 CORS 的使用方式,我已经在其他项目上以这种方式运行它(但从未在 ASP.NET Core 2 中)。

Thank you in advance先感谢您

This may not be an answer to your situation, but I've run into this issue quite often in the past.这可能不是您的情况的答案,但我过去经常遇到这个问题。 It's a Chrome issue when talking between two localhost s upon POST requests.根据POST请求在两个localhost之间交谈时,这是一个Chrome问题。

Try using another browser and see if it works;尝试使用其他浏览器,看看它是否有效; if so, you can continue using Chrome by disabling Security如果是这样,您可以通过禁用Security来继续使用Chrome

chrome.exe --user-data-dir="C://Chrome dev session" --disable-web-security

hope this helps.希望这可以帮助。

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

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