December 1, 2016 08:05 by
Peter
As with the most things in ASP.NET MVC 6, just about everything is handled within your Startup.cs file. With this tutorial, you will set up all your necessary routing, services, dependency injection, and more. And setting an expiration for a persistent cookie, turns out to be no different.
To set your persistent cookie expiration, you will need to associate the cookie to your current Identity provider. This is handled within the ConfigureServices method of the previously mentioned Startup.cs file, as you can see on the following code:
public void ConfigureServices(IServiceCollection services)
{
// Add Entity Framework services to the services container along
// with the necessary data contexts for the application
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:IdentityConnection:ConnectionString"]))
.AddDbContext<YourOtherContext>(options => options.UseSqlServer(Configuration["Data:DataConnection:ConnectionString"]));
// Add Identity services to the services container
services.AddIdentity<ApplicationUser, IdentityRole>(i => {
i.SecurityStampValidationInterval = TimeSpan.FromDays(7);
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Other stuff omitted for brevity
}
You might have noticed after a quick peek at this code what exactly you need to be setting. That's right. The SecurityStampValidationInterval property:
// This will allow you to set the duration / expiration of your
// authentication token
i.SecurityStampValidationInterval = TimeSpan.FromDays(7);
This example would only require the users to re-validate if they have not logged into the application within seven days. You can simply adjust this interval value to suit your needs.
HostForLIFE.eu ASP.NET MVC 6 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.