I am using auth0 of my mvc application, and I want to retrieve the email but I find it in claims and its value during execution is null.
here is my code
public async Task Callback()
{
var result = await HttpContext.AuthenticateAsync();
if (result?.Principal != null)
{
// Extraire l’email depuis les claims si disponible
var email = result.Principal.FindFirst(“email”)?.Value;
var sub = result.Principal.FindFirst(“Name”)?.Value;
if (email != null)
{
ViewBag.Email = email; // Optionnel : stockez l'email pour l'utiliser dans la vue
ViewBag.Message = "Connexion réussie avec email !";
}
else
{
ViewBag.Message = "Connexion réussie, mais l'email est introuvable.";
}
return View("Success");
}
ViewBag.Message = "Erreur d'authentification.";
return View("Error");
}
I can recover name and nickname but not email
Thank you for your help
Hi @monagi.jalil,
Welcome to the Auth0 Community!
To have the email in your token claims, please append them as a custom claim.
Please follow the instructions in our Adding custom claims to tokens knowledge article.
Here is another documentation for your reference: Create Custom Claims
Thanks,
Rueben
Thank you Rueben first for the response.
in action\library I created an action from scratch and I deployed here is my code:
exports.onExecutePostLogin = async (event, api) => {
// Vérifiez si l’utilisateur a un email
if (event.user.email) {
// Ajoutez un claim personnalisé avec l’email dans le token
api.accessToken.setCustomClaim(‘https://localhost:7296//email’, event.user.email);
}
};
Are there any steps left to take to attach it to my application?
thank you once again
i tried also this code in the trigger :
exports.onExecutePostLogin = async (event, api) => {
const namespace = ‘https://localhost:7296/’;
const { email } = event.user.user_metadata;
if (event.authorization) {
// Set claims in ID token
api.idToken.setCustomClaim(`${namespace}/email`, email);
// Set claims in access token
api.accessToken.setCustomClaim(`${namespace}/email`, email);
}
};
Hi @monagi.jalil,
Thanks for the update!
Your action script looks good. You need to make sure to attach the action script to the post-login flow and apply those changes.
Cheers,
Rueben