Login CORS issue with ASP.NET and Angular 7

I have an issue integrating the aspnet Core 2.1 quickstart with angular 7.
I have an Account Controller and an authService.

The Account Controller is called via the angular front end authService.

If I call the controller (login method) using httpClient the login function throws a CORS error.

If I call the controller (login method) via the root, directly in the URL bar, the function executes just fine.

My Authenticate function is able to be called by the front end service, but the logout has the same issue.

Here is a link to my gitHub containing the Controller, front-end Service, Startup.CS and URL Service.
GitHub

Below are snips of the files in case youd like to view them in the post:


if anyone could help me resolve my issue in calling my login function from the front-end it would be greatly appreciated!

When the Request comes from some other domain, it throws an error. It means, the browser has a property called Access-Control-Allow-Origin which restricts the requests from different domains for security purposes. So, you need to enable CORS to accomplish the request. Just a simple change in your asp.net WebAPI project’s web.config is working great:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
        </customHeaders>
    </httpProtocol>
</system.webServer>
1 Like

Thanks for sharing that with the rest of community!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.