Increase token lifespan in .net core 3.1

Viewed 1568

How to configure the TokenLifespan of a token generated in .NET Core 3.1? I am using the following code but it's not working and is still using the 1-day TokenLifespan which is the default value.

public void ConfigureServices(IServiceCollection services){
    //...
    services.Configure<DataProtectionTokenProviderOptions>(options =>
    {
        options.TokenLifespan = TimeSpan.FromDays(5);
    });
    //...
}

I am not interested in configuration through custom classes when there is a builtin option to change it through the code given above but I don't know what am I doing wrong which is overriding my defined TokenLifeSpan value.

2 Answers

If you are using a provider that is based on TotpSecurityStampBasedTokenProvider i.e. EmailTokenProvider or PhoneNumberTokenProvider, then I don't think this is possible.

The Totp provider produces 6 digit codes, and has a life span of 3 minutes with a 90 seconds variance.

You would probably have to find a way to create your own OTP if you require them to be longer.

EDIT

Have a look at this post on how you can override the Rfc6238AuthenticationService to the extended/decrease your token lifespan

Related