# Decode access token in backend

**URL:** <https://community.auth0.com/t/decode-access-token-in-backend/86887>\
**Category:** Get Help\
**Created:** [July 13, 2022, 11:37am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887 "2022-07-13T11:37:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnthonyDaSilva](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/anthonydasilva/32/33385_2.png) [@AnthonyDaSilva](https://community.auth0.com/u/AnthonyDaSilva)\
**Post date:** [July 13, 2022, 11:37am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/1 "2022-07-13T11:37:45Z")

</div>

Hi,

I implemented the authentication in the front end as such :

```auto
userLogin: function() {    
    let deferred = new Ext.Deferred();
    //Create auth0 client
    //TODO ADA : move domain and client_id to auth_config.json
    createAuth0Client({
      domain: "domain",
      client_id: "clientId",
      audience: "audience",
      useRefreshTokens: true
    }).then(function(auth0) {
        try {
            auth0.isAuthenticated().then(async function (authenticated) {
                if (!authenticated) {
                    const query = window.location.search;
                    const shouldParseResult = query.includes("code=") && query.includes("state=");
                    if (shouldParseResult) {
                        console.log("> Parsing redirect");
                        try {
                            const result = await auth0.handleRedirectCallback();
                            console.log("Logged in!");
                            deferred.resolve();
                        } catch (err) {
                            console.log("Error parsing redirect:", err);
                            deferred.reject();
                        }
                        window.history.replaceState({}, document.title, "/");
                    } else {
                        auth0.loginWithRedirect({ redirect_uri: window.location.origin });
                        deferred.resolve();
                    }
                } else {
                    auth0.getTokenSilently().then(function (token) {
                        debugger;
                        Ext.Ajax.setDefaultHeaders({ 'Authorization': 'Bearer ' + token });
                        deferred.resolve();
                    });
                }
            })
        } catch (err) {
            console.log("Log in failed", err);
            deferred.reject();
        }            
    });

    return deferred.promise;
}

```

And I configured my API using this [doc](https://auth0.com/docs/quickstart/backend/webapi-owin/interactive) :

```auto
var domain = $"domain";
var apiIdentifier = "identifier";
var keyResolver = new OpenIdConnectSigningKeyResolver(domain);

app.UseJwtBearerAuthentication(
    new JwtBearerAuthenticationOptions
    {
        AuthenticationMode = AuthenticationMode.Active,
        TokenValidationParameters = new TokenValidationParameters()
        {
            ValidAudience = apiIdentifier,
            ValidIssuer = domain,
            IssuerSigningKeyResolver = (token, securityToken, kid, parameters) => keyResolver.GetSigningKey(kid)
        }
    });

```

Now I need to Identify my user in the back end to load his preferences etc.  
How can I do this?

I tried to decode the token ([https://jwt.io/](https://jwt.io/)) but it seems to be empty or malformed.

Thanks

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 14, 2022, 12:48am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/3 "2022-07-14T00:48:35Z")

</div>

Hello @AnthonyDaSilva!

> [@AnthonyDaSilva](#):
>
> I tried to decode the token ([https://jwt.io/](https://jwt.io/)) but it seems to be empty or malformed.

Interesting - Can you confirm you are passing a valid audience? Without that, the access token you get back can be opaque.

> [@AnthonyDaSilva](#):
>
> Now I need to Identify my user in the back end to load his preferences etc.  
> How can I do this?

It sounds like you will either want to look at getting an ID token or using the access token received to call [/userinfo](https://auth0.com/docs/api/authentication#get-user-info). Both are valid approaches 😄

---

<div class="post-metadata">

**Author:** ![AnthonyDaSilva](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/anthonydasilva/32/33385_2.png) [@AnthonyDaSilva](https://community.auth0.com/u/AnthonyDaSilva)\
**Post date:** [July 14, 2022, 5:31am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/4 "2022-07-14T05:31:08Z")

</div>

Hello @ty.frith,

I changed the audience and now the decoding is working. 🙂  
For the user info, I implemented the /userinfo call but it seems it’s not the proper way of doing it as I’m reaching the request limit pretty easily (The remote server returned an error: (429) Too Many Requests.).

How can I get the ID Token ?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 22, 2022, 12:24am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/5 "2022-07-22T00:24:50Z")

</div>

Sorry for the delayed response here, but wanted to follow up!

> [@AnthonyDaSilva](#):
>
> I changed the audience and now the decoding is working. 🙂

Awesome, good to know this is working for you now 😄

> [@AnthonyDaSilva](#):
>
> How can I get the ID Token ?

Were you able to get this sorted? An ID token should be returned if you include the `openid` scope as a param alongside `domain`, `client_id` etc.

---

<div class="post-metadata">

**Author:** ![AnthonyDaSilva](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/anthonydasilva/32/33385_2.png) [@AnthonyDaSilva](https://community.auth0.com/u/AnthonyDaSilva)\
**Post date:** [July 22, 2022, 5:29am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/6 "2022-07-22T05:29:56Z")

</div>

Hi tyf,

I managed to add the user email in the access token and it’s working, but I have another question, the parameter name for the rule is ‘[Http://example.com/](Http://example.com/)’ and if I change it disappear from the access token.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 6, 2022, 5:29am UTC](https://community.auth0.com/t/decode-access-token-in-backend/86887/7 "2022-08-06T05:29:58Z")

</div>

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