# Have email in claims

**URL:** <https://community.auth0.com/t/have-email-in-claims/151075>\
**Category:** Get Help\
**Tags:** configuration, application\
**Created:** [November 6, 2024, 7:48pm UTC](https://community.auth0.com/t/have-email-in-claims/151075 "2024-11-06T19:48:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![monagi.jalil](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/monagi.jalil/32/62001_2.png) [@monagi.jalil](https://community.auth0.com/u/monagi.jalil)\
**Post date:** [November 6, 2024, 7:48pm UTC](https://community.auth0.com/t/have-email-in-claims/151075/1 "2024-11-06T19:48:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [November 6, 2024, 10:37pm UTC](https://community.auth0.com/t/have-email-in-claims/151075/3 "2024-11-06T22:37:27Z")

</div>

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](https://community.auth0.com/t/adding-custom-claims-to-tokens/84590) knowledge article.

Here is another documentation for your reference: [Create Custom Claims](https://auth0.com/docs/secure/tokens/json-web-tokens/create-custom-claims)

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![monagi.jalil](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/monagi.jalil/32/62001_2.png) [@monagi.jalil](https://community.auth0.com/u/monagi.jalil)\
**Post date:** [November 7, 2024, 2:32am UTC](https://community.auth0.com/t/have-email-in-claims/151075/4 "2024-11-07T02:32:16Z")

</div>

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](https://localhost:7296//email)’, event.user.email);  
}  
};

Are there any steps left to take to attach it to my application?

thank you once again

---

<div class="post-metadata">

**Author:** ![monagi.jalil](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/monagi.jalil/32/62001_2.png) [@monagi.jalil](https://community.auth0.com/u/monagi.jalil)\
**Post date:** [November 7, 2024, 3:36am UTC](https://community.auth0.com/t/have-email-in-claims/151075/5 "2024-11-07T03:36:56Z")

</div>

i tried also this code in the trigger :

exports.onExecutePostLogin = async (event, api) =\> {

const namespace = ‘[https://localhost:7296/](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);

```

}

};

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [November 8, 2024, 8:06pm UTC](https://community.auth0.com/t/have-email-in-claims/151075/6 "2024-11-08T20:06:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [November 22, 2024, 8:06pm UTC](https://community.auth0.com/t/have-email-in-claims/151075/7 "2024-11-22T20:06:06Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
