0

My problem is that I am trying to login to asp.net core backend with angular2.

The response contains token and is 200 OK, but always the error called with status 0 unknown error and this error appears:

Failed to load http://localhost:5000/api/token: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

so any help.

Robert
  • 5,703
  • 2
  • 31
  • 32
Montaser
  • 57
  • 5
  • 2
    Possible duplicate of [AngularJS: No "Access-Control-Allow-Origin" header is present on the requested resource](https://stackoverflow.com/questions/29547003/angularjs-no-access-control-allow-origin-header-is-present-on-the-requested-r) – bugs Apr 17 '18 at 10:04
  • it is not angularjs. Although it is a server side problem indeed. – ForestG Apr 17 '18 at 10:14
  • FrestG so what is the solution? – Montaser Apr 17 '18 at 10:19
  • @Montaser In production environment your apps (Angular & Asp.Net Core app) will run under the same domain or different domain? Please clarify. – parag Apr 17 '18 at 12:56

1 Answers1

0

This is CORS problem. You need to allow CORS so you can call your api service from different domain. Currently you are trying to call http://localhost:5000 from http://localhost:4200 which is not allowed since theese are different domains. In your API service aplication go to Startup.cs file and in your ConfigureServices method you must configure CORS. Try something like this:

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

Hope it helps!

Milos Kovacevic
  • 861
  • 1
  • 9
  • 20
  • i am facing same issue, please can u specify more where the API service application and the Startup.cs file? i can't find them, Thanks, i am using php as serve side language – Ali Aug 06 '18 at 10:21
  • Hi Aly! This answer was intended for people that use asp.net for backend. Since you are using PHP as your backend technology there is no Startup.cs class in your project. Read this https://stackoverflow.com/questions/8719276/cors-with-php-headers – Milos Kovacevic Aug 07 '18 at 08:22