How to set TimeOut for OwinContext in MVC 5 How to set TimeOut for OwinContext in MVC 5 asp.net asp.net

How to set TimeOut for OwinContext in MVC 5


The way to set an fixed expiration time span is to set the ExpireTimeSpan property in your Startup.Auth.cs file like this:

// Enable the application to use a cookie to store information for the signed in userapp.UseCookieAuthentication(new CookieAuthenticationOptions{    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,    LoginPath = new PathString("/Account/Login"),    ExpireTimeSpan = TimeSpan.FromDays(2)});

Note that you'll also have to set the cookie to persist. In your code you'll have to pass in a bool in addition to the username and password, and then change

authenticationManager.SignIn(id); 

to be

authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, id); 


With the following you do not need to use Startup.cs

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddHours(1), }, id);