So I have a NextJS frontend set up with the quickstart from Auth0 Website. I then send to a nextjs API route where I call
const { accessToken } = await getAccessToken(req, res);
I want to then send this access token to my .NET API where I can then find the user ID associated with the token. I have tried a few things for this. Firstly I did not create a Auth0 API application inside auth0 portal and just added this code:
public async Task<string?> GetUserIdFromToken(string token)
{
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(“kLgf6y-lohC4VxD”)),
ValidateIssuer = true,
ValidIssuer = $“https://{“dev-g437ixl7.us.auth0.com”}/”,
ClockSkew = TimeSpan.Zero,}; try { var claimsPrincipal = tokenHandler.ValidateToken(token, validationParameters, out var validatedToken); var userIdClaim = claimsPrincipal.FindFirst(ClaimTypes.NameIdentifier); return userIdClaim?.Value; } catch(Exception ex) { return null; } }
This did not work so I then tried creating an Auth0 API in the portal, I then added my web application to the authorised list and followed the quickstart:
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = “https://dev-g437ixl7.us.auth0.com/”;
options.Audience = “https://customer-analytics-api”;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidAlgorithms = new { “dri”, “RS512” }
};
});
With this I can generate a token using the API endpoint and it will auth to my API but if I try and use the token created in nextjs it doesn’t work. Can someone explain what I am doing wrong and what I need to do to achieve my goal? Cheers