Jun 26, 2016

How to change Asp.net Identity token expiration in Asp.Net Core or MVC 6

Asp.Net Identity default expiration time is 1 day. Some time its require to change based on our requirement.

Default property initialization on DataProtectionTokenProviderOptions class is
 public TimeSpan TokenLifespan { get; set; } = TimeSpan.FromDays(1);

Require customization for change to default token expiration time.

Following step require to change default expiration token time.

Step 1 : Create new class with name DefaultDataProtectorTokenProviderOptions and Inherit from DataProtectionTokenProviderOptions class.
public class DefaultDataProtectorTokenProviderOptions : DataProtectionTokenProviderOptions { }

Step 2 : Create new class with name DefaultDataProtectorTokenProvider and Inherit from DataProtectorTokenProvider class.
public class DefaultDataProtectorTokenProvider : DataProtectorTokenProvider where TUser : class
    {
        public DefaultDataProtectorTokenProvider(IDataProtectionProvider dataProtectionProvider, IOptions options) : base(dataProtectionProvider, options)
        {
        }
    }
Step 3 : Time to use above classes on Startup class and AddTokenProvider.
Step 4: Declare private variable for provider name
 private const string _defaultTokenProviderName = "Default";

Step 5 : Under ConfigureServices method add following code for token expiration time change.


Here you can see set time span for token expiration.
// Set time span for token expiration time
           
                options.TokenLifespan = TimeSpan.FromMinutes(15);
           

You can get the source code from GitHub.

Hope you like this so please keep reading.

No comments:

Post a Comment