-1
  • Frontend: Angular
  • Backend: .NET core API
  • Environment: MS Azure;

Requirement: We want .NET core API to accept requests from authorized UI application who are having access token. We are current trying to get the access token so that we can include in header and send to .NET core API, but we are facing an issue that while running the angular application locally we are unable to get access token from online.Microsoftonline.com instead the infamous CORS error.

What is wrong we may be doing?

Scorpian275
  • 416
  • 3
  • 6
  • https://stackoverflow.com/a/70230578/15969115 and https://stackoverflow.com/questions/60508127/how-to-pass-token-in-header-section-in-authorization-field-in-angular – Ecstasy Jun 08 '22 at 04:37
  • @DeepDave-MT thanks for the links, we are aware of interceptors and CORS. But while requesting for token from Angular we were getting cors error from Microsoft website – Scorpian275 Jun 08 '22 at 07:48
  • https://stackoverflow.com/questions/60980143/how-to-fix-cors-error-on-angular-project-deployed-on-an-azure-web-app-pointing-t and https://learn.microsoft.com/en-us/answers/questions/735355/cors-issue-while-calling-net-core-api-and-angular.html – Ecstasy Jun 08 '22 at 07:50
  • @DeepDave-MT Thanks for the links. May be Microsoft link may help – Scorpian275 Jun 13 '22 at 14:32

1 Answers1

0

we are aware of interceptors and CORS. But while requesting for token from Angular we were getting cors error from Microsoft website.

There are few of the workarounds,

Make sure that your API is not connected with another domain which can be reason for not able to get access from CORs policy ,so may be for that reason you will not be able to call the API.

For example to enable CORs attribite ;

[EnableCors(origins: "http://webapp.net ", headers: "*", methods: "*")]
      public class TestController : ApiController
      {
          // Controller methods not shown...
      }
  }

For more information please refer this MS Q&A discussion which is suggested by @DeepDave-MT in comment.

And, this Blog|ANGULAR SPA WITH AN ASP.NET CORE API USING AZURE AD AUTH AND USER ACCESS TOKENS.

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15