Skip to content
Advertisement

Sending Authorization Token Bearer through JQuery Ajax – Back end is .NET Core Web Api

I am having a 401 error code when I access to the api using Jquery Ajax. This is my front-end code:

JavaScript

For my back-end, I am using ASP.NET Identity

JavaScript

and my startup.cs code is

JavaScript

I have tested. Without [Authorize] attribute, I can see the result response at the client side. But once I have added [Authorize] attribute and added headers property in CallAPI method, it hits 401. I have tested https://localhost:44342/api/Authenticate/login and it works as expected. It returns the token. But when I use the token to call ‘https://localhost:44342/api/Customers’, it hits 401

Advertisement

Answer

But once I have added [Authorize] attribute and added headers property in CallAPI method, it hits 401.

Based on the code of Startup.cs that you shared, we can find that you configured multiple authentication mechanisms in your project.

As we discussed in comments, we can specify the authentication scheme (or schemes) with the [Authorize] attribute they depend on to authenticate the user.

JavaScript

For more information about selecting the scheme with the Authorize attribute, please check this doc:

https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-3.1#selecting-the-scheme-with-the-authorize-attribute

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement