I am building a react application, using Auth0 as my oauth authentication & backing it with a ASP.NET core api.
From React, I redirect the user to Auth0, which I have setup a single page application application.
When the login is succesful, it redirects the user back to my React app with a code.
I then want to translate that code into a JWT token to authorise access of the backend api, This is where it fails.
After login, I use the Auth0 supplied example library and call:
const { getTokenSilently } = useAuth0();
...
var token = await getTokenSilently();
...
axios({
url: `/api/Folder`,
method: 'GET',
headers: {
Authorization: `Bearer ${token}`
}
})
it does provide a token but the token seems too small for a JWT Token, it looks something like this in the header:
Authorization: Bearer 7hExNvsOM14TpY0qUnPbVqpizwLLxynw
The response from my C# asp.net core api is:
www-authenticate: Bearer error="invalid_token"
My C# Code looks like this: startup.cs
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = Configuration["Auth0:Authority"];
options.Audience = Configuration["Auth0:Audience"];
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = ClaimTypes.NameIdentifier
};
});
IdentityModelEventSource.ShowPII = true; //for debug purposes
....
app.UseAuthentication();
app.UseAuthorization();
folderController.cs
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class FolderController : ControllerBase
{
....
}