Search code examples
asp.net-corejwtasp.net-identity.net-8.0asp.net-minimal-apis

How do I configure accessToken lifetime in ASP.NET Core 8 Minimal API using new IdentityApi endpoints?


I've just started working on a new ASP.NET Core 8 Minimal API. I'm trying to play with some new features regarding identity.

I'm using the class below to register auth services.

internal static class AuthInstaller
{
    internal static IServiceCollection InstallAuth(this IServiceCollection services)
    {
        services
            .AddAuthorization()
            .AddIdentityApiEndpoints<ApplicationUser>()
            .AddEntityFrameworkStores<DatabaseContext>();

        return services;
    }
}

Then in my endpoints installer class, I'm invoking MapIdentityApi to map identity endpoints as follows.

...
groupBuilder.MapIdentityApi<ApplicationUser>();
...

I have it all working, but the accessToken lifetime is set to 3600 seconds. I've spent much time trying to figure out on how to configure that lifetime. Is there a quick way to configure that without having to write custom bearer token creation logic?

I've looked through Microsoft documentation, but couldn't find anything helpful.


Solution

  • You need to add code

    ...
    builder.Services.AddIdentityApiEndpoints<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
    
    builder.Services.ConfigureAll<BearerTokenOptions>(option => {
        option.BearerTokenExpiration = TimeSpan.FromMinutes(1);
    });
    ...