How to change JWT timeout for dotnet core SPA (Angular) with Identity Server

Viewed 728

I'm using dotnet core 3, and trying to set the JWT expire time, and can't find where to do so. I know it can be done where the JWT token is created, but that's happening in one of Microsoft's libraries. Anybody out there know how this can be accomplished? Alternatively it would be nice to have a system to automatically refresh the token.

Here are the relevant excerpts from my Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
   services.AddDbContext<ApplicationDbContext>(options =>
      options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));

   services.AddDefaultIdentity<ApplicationUser>(options => 
      options.SignIn.RequireConfirmedAccount = true)
      .AddEntityFrameworkStores<ApplicationDbContext>();

   services.AddIdentityServer()
      .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

   services.AddAuthentication()
      .AddIdentityServerJwt();

   services.Configure<JwtBearerOptions>(IdentityServerJwtConstants.IdentityServerJwtBearerScheme, options =>
   {
      //Wouldn't this be a good place for an expiration property? Microsoft doesn't think so.
   });

   //Unrelated service configuration code here
}

Then in Configure:

public void Configure(...){
   //Irrelevant stuff here

   app.UseAuthentication();
   app.UseIdentityServer();
   app.UseAuthorization();

   //More irrelevant stuff here
}

Any help is greatly appreciated. Thanks!

1 Answers

You can re-config the default Client that Microsoft created in AddApiAuthorization method

services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>(options =>
{
    options.Clients.First().AccessTokenLifetime = 600;
});
Related