# Id token validation incase of MVC application

**URL:** <https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [December 12, 2022, 12:22pm UTC](https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260 "2022-12-12T12:22:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pradeepta.meher](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pradeepta.meher/32/33650_2.png) [@pradeepta.meher](https://community.auth0.com/u/pradeepta.meher)\
**Post date:** [December 12, 2022, 12:22pm UTC](https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260/1 "2022-12-12T12:22:29Z")

</div>

Hi Team,  
I have a Sample MVC application downloaded from Auth0. The configuration is the default configuration there with the sample application.  
Included the below middleware  
app.UseAuthentication();  
app.UseAuthorization();  
Performing the following steps:

1. Ran and logged in to the application successfully.
2. Later I went to my Auth0 account and changed the Id token expiration time to 10 sec in my application configuration.
3. Now I ran my sample application again, logged in and wait for 15 second.
4. After 15 second I browsed to an [Authorize] end point.
5. Expected: Application should show some token expiration message and should not allow to browse to [Authorize] end point.  
Actual: Application is not showing any token expiration message and allow to browse to [Authorize] end point.  
Please provide suggestion on how to validate an Id token in MVC application, so that application should not allow to browse to [Authorize] end point.

---

<div class="post-metadata">

**Author:** ![lihua.zhang](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/lihua.zhang/32/39683_2.png) [@lihua.zhang](https://community.auth0.com/u/lihua.zhang)\
**Post date:** [December 16, 2022, 12:58am UTC](https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260/4 "2022-12-16T00:58:29Z")

</div>

Hi @pradeepta.meher ,

I noticed that you opened a Support ticket for the same issue. Once our DSE solves it, I will share the details on this topic to make it beneficial to other folks in our Community as well!

Thanks!

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 9, 2023, 1:47pm UTC](https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260/5 "2023-02-09T13:47:06Z")

</div>

We use the default cookie middleware, and the OIDC package from aspnetcore. Our SDK doesnt handle any of that.The Microsoft.AspNetCore.Authentication.Cookies package allows u to specify a SessionStore (which implements ITicketStore) that can be used to store things server side in a session. If not, everything goes in the cookie."

> **[aspnetcore/src/Security/Authentication/Cookies at main · dotnet/aspnetcore](https://github.com/dotnet/aspnetcore/tree/main/src%2FSecurity%2FAuthentication%2FCookies)**
>
> main/src%2FSecurity%2FAuthentication%2FCookies

We register the cookie middleware and use default config, but u can still configure the cookie middleware the way you like. If you would like to configure the Microsoft.AspNetCore.Authentication.Cookie middleware to use a session, you can configure it as such:

```auto
public void ConfigureServices(IServiceCollection services)
{
     services
               .AddAuth0WebAppAuthentication(PlaygroundConstants.AuthenticationScheme, options =>
               {
                   options.Domain = Configuration["Auth0:Domain"];
                   options.ClientId = Configuration["Auth0:ClientId"];
                   options.ClientSecret = Configuration["Auth0:ClientSecret"];
               });
     // Configure a custom ITicketStore to store the Identity Information on the server
     services.AddTransient<ITicketStore, CustomInMemoryTicketStore>();
     // Configure the Cookie Middleware to use the CustomInMemoryTicketStore
     services.AddSingleton<IPostConfigureOptions<CookieAuthenticationOptions>, ConfigureCookieAuthenticationOptions>();
}

```

and

```auto
public class ConfigureCookieAuthenticationOptions
  : IPostConfigureOptions<CookieAuthenticationOptions>
    {
        private readonly ITicketStore _ticketStore;

        public ConfigureCookieAuthenticationOptions(ITicketStore ticketStore)
        {
            _ticketStore = ticketStore;
        }

        public void PostConfigure(string name,
                 CookieAuthenticationOptions options)
        {
            options.SessionStore = _ticketStore;
        }
    }

```

Every request will then call the below TicketStore’s “RetrieveAsync” method:

```auto
public class CustomInMemoryTicketStore : ITicketStore
    {
        private readonly IMemoryCache _cache;

        public CustomInMemoryTicketStore(IMemoryCache cache)
        {
            _cache = cache;
        }

        public Task RemoveAsync(string key)
        {
            _cache.Remove(key);

            return Task.CompletedTask;
        }

        public Task<AuthenticationTicket> RetrieveAsync(string key)
        {
            var ticket = _cache.Get<AuthenticationTicket>(key);

            return Task.FromResult(ticket);
        }

        public Task RenewAsync(string key, AuthenticationTicket ticket)
        {
            _cache.Set(key, ticket);

            return Task.CompletedTask;
        }

        public Task<string> StoreAsync(AuthenticationTicket ticket)
        {
            var key = ticket.Principal.Claims
              .First(c => c.Type == "sid").Value;

            _cache.Set(key, ticket);

            return Task.FromResult(key);
        }
    }

```

The above TicketStore simply stores in memory, but shows how you can add in a session layer by configuring the Microsoft.AspNetCore.Aurthentication.Cookies middleware.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 22, 2023, 11:24am UTC](https://community.auth0.com/t/id-token-validation-incase-of-mvc-application/96260/6 "2023-02-22T11:24:15Z")

</div>


