title: Refresh id token before it expires in aspnet core mvc
body: <p>I am working on a aspnet core MVC project which calls an API which is secured by OAuth2.0.</p>
  
  <p>I was able to access the API and get the response properly with the code below,</p>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>        public void ConfigureServices(IServiceCollection services)
          {
          services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
          {
              options.CheckConsentNeeded = context =&gt; true;
              options.MinimumSameSitePolicy = SameSiteMode.None;
          });
  
          services.AddAuthentication(options =&gt;
              {
                  options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                  options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                  options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              })
              .AddCookie()
              .AddOpenIdConnect(options =&gt;
              {
                  options.Authority = Configuration["AzureAD:Instance"] + 
                   "/" + Configuration["AzureAD:TenantId"];
                  options.ClientId = Configuration["AzureAD:ClientId"];
                  options.Secret = Configuration["AzureAD:Secret"];
                  options.Callback = Configuration["AzureAD:Callback"];
                  options.ResponseType = "code id_token";
                  options.SaveTokens = true;
              });
  
          services.AddMvc(options =&gt;
              {
                  var policy = new AuthorizationPolicyBuilder()
                      .RequireAuthenticatedUser()
                      .Build();
                  options.Filters.Add(new AuthorizeFilter(policy));
              })
              .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
      }
  
   public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          ...
          app.UseCookiePolicy();
          app.UseAuthentication();
          app.UseMvc(routes =&gt;
          {
              routes.MapRoute(
                  name: "sign-in",
                  template: "signin-oidc")};
  
  
              ...
  
      });
      }
  </code></pre>
  
  <p><strong>Controller.cs</strong></p>
  
  <p>I am getting the token from the HttpContext.</p>
  
  <p>But the token expires after few hours. Is there a way to refresh the id token before it expires.
  I tried few examples in stack overflow. But none worked for me.</p>
  
  <p>Is there a way to get this to work without using the IdentityModel used in example below?
  <a href="https://github.com/mderriey/aspnet-core-token-renewal/blob/master/src/MvcClient/Startup.cs" rel="nofollow noreferrer">https://github.com/mderriey/aspnet-core-token-renewal/blob/master/src/MvcClient/Startup.cs</a></p>