I have a NextJS app which is using the quickstart guide to set up auth 0.
I have the following env vars
AUTH0_SECRET=123
AUTH0_BASE_URL=http://localhost:3000
AUTH0_ISSUER_BASE_URL='https://dev-1234.us.auth0.com'
AUTH0_CLIENT_ID=12345
AUTH0_CLIENT_SECRET=123456
AUTH0_AUDIENCE=
AUTH0_SCOPE='openid profile'
In my nextjs API route I am getting the token and sending it to a .NET rest api.
export default withApiAuthRequired(async function users(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const { accessToken } = await getAccessToken(req, res);
const { path } = req.query;
const response = await axios.get(`https://localhost:7230/Reports/${path}`, {
headers: { Authorization: `Bearer ${accessToken}` },
httpsAgent: new https.Agent({
rejectUnauthorized: false,
}),
});
res.status(200).json(response.data);
} catch (error: any) {
console.error(error);
res.status(error.status || 500).end(error.message);
}
});
In my rest API I am trying to validate the token and get the auth0 id. Here is the code.
public async Task<string?> GetUserIdFromToken(string token)
{
var tokenHandler = new JwtSecurityTokenHandler();
var validationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("123")),
ValidateIssuer = true,
ValidIssuer = $"https://{"dev-1234.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;
}
}
I have tried many variants of the code below above but nothing seems to work. Right now I am getting
{"IDX10609: Decryption failed. No Keys tried: token: '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'."}
but I am also getting lots of other types of errors if I try alternatives. Can anyone spot what I am doing wrong?