0

I used the ClaimsIdentity Framework to set up auth in one of my applications.

I added those lines into the web.config file like decribed in this question.

<sessionState
         mode="InProc"
         timeout="1" />

I let the application run overnight, but I was still logged in. I'd like to set the session timeout to 30 minutes, any suggestions ?

ASP.NET MVC Version: 5.2.3.0

AME
  • 2,262
  • 6
  • 19
  • 39

1 Answers1

2

Per ASP.Net-Identity-Cookie-Authentication-Timeouts you should be using Identity's UseCookieAuthentication() parameters to set the timeout.

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  LoginPath = new PathString("/Account/Login"),
  Provider = new CookieAuthenticationProvider
  {
    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
        validateInterval: TimeSpan.FromMinutes(15),
        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
  },
  SlidingExpiration = false,
  ExpireTimeSpan = TimeSpan.FromMinutes(30)
});

CookieAuthenticationOptions.ExpireTimespan is the option that allows you to set how long the issued cookie is valid for. In the example above, the cookie is valid for 30 minutes from the time of creation. Once those 30 minutes are up the user will have to sign back in becuase the SlidingExpiration is set to false.

If SlidingExpiration is set to true then the cookie would be re-issued on any request half way through the ExpireTimeSpan. For example, if the user logged in and then made a second request 16 minutes later the cookie would be re-issued for another 30 minutes. If the user logged in and then made a second request 31 minutes later then the user would be prompted to log in.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • is it possible to move these settings from Startup.Auth.cs code to the web.config file (or at least the ExpireTimeSpan alone)? – Alexander Mihailov Nov 16 '20 at 20:47
  • @AlexanderMihailov What would prevent you from reading the web.config settings into variables before this code and using those variables to dynamically provide the values for this code? – Erik Philips Nov 17 '20 at 10:32