Sabari M Sabari M
Updated date Feb 07, 2022
ASP.NET MVC Session state is used to temporarily store and retrieve the values for a user when the user navigates to another view in an ASP.NET MVC application. In this blog, we will see how to increase the session timeout in ASP.NET MVC Application.
  • 21.4k
  • 0
  • 0

ASP.NET MVC Session state is used to temporarily store and retrieve the values for a user when the user navigates to another view in an ASP.NET MVC application. 

Usually, the session timeout configuration setting will be stored in the web.config file in the MVC application. If you want to increase the session timeout then open your application web.config file which is placed under your application root folder.

Add  <sessionState timeout="300"></sessionState> code under <system.web>   </system.web> the section as shown below, In this example, I have updated the timeout to 30 min

<system.web>   
    <sessionState timeout="30"></sessionState>
</system.web>

By default, the ASP.NET MVC session timeout value is 20 minutes.

For automatic Logout from Asp.net Identity when the user is Inactive, you have to use the below code in the Startup.cs file. The login page will be automatically redirected after 30 mins if the user is inactive.

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    SlidingExpiration = true,
	AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
	LoginPath = new PathString("/Account/Login"),
	Provider = new CookieAuthenticationProvider
	{
		// Enables the application to validate the security stamp when the user logs in.
		// This is a security feature that is used when you change a password or add an external login to your account.  
		OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
			validateInterval: TimeSpan.FromMinutes(30),
			regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
	}
});        

 

ABOUT THE AUTHOR

Sabari M
Sabari M
Software Professional, India

IT professional with 15+ years of experience in Microsoft Technologies with a strong base in Microsoft .NET (C#.Net, ASP.Net MVC, ASP.NET WEB API, Webservices,...Read More

https://www.techieclues.com/profile/alagu-mano-sabari-m

Comments (0)

There are no comments. Be the first to comment!!!