How to increase session timeout in ASP.NET MVC
By FoxLearn 1/9/2025 7:07:15 AM 887
What is the idle timeout for ASP.NET session?
The timeout is measured in minutes, with a default of 20 minutes and a maximum limit of 1 year. However, if you host the site on a shared Windows server, your log-in may expire much sooner than this setting.
To increase the session timeout in an ASP.NET MVC application, you need to adjust the session state settings in your web.config file.
How to implement session timeout in ASP.NET MVC?
Open the web.config file of your ASP.NET MVC project, then increase the value in minutes by using the time out attribute of SessionState element.
<!-- asp.net session mvc timeout --> <system.web> <sessionState timeout="300"></sessionState> </system.web>
By default, the session timeout value is 20 minutes. Also in your case if you are using forms authentication, please check the timeout value.
By setting the timeout
attribute to a specific value, you're specifying the number of minutes the session remains active without any activity from the user. Adjust the value according to your application's requirements.
<system.web> <authentication mode="Forms"> <forms loginUrl="~/Account/Login" protection="All" path="/" timeout="300" /> </authentication> </system.web>
Open IdentityConfig.cs, then change the value of the account logout time span that you want to change.
public class ApplicationUserManager : UserManager<CustomUser> { public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); // Configure user lockout defaults manager.UserLockoutEnabledByDefault = true; manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromDays(1); manager.MaxFailedAccessAttemptsBeforeLockout = 5; return manager; } }
How to get session timeout value in ASP.NET C#?
In the code behind, you can add the following code to check the session timeout value.
// Get the session timeout value int sessionTimeout = Session.Timeout;
- Content Negotiation in Web API
- How to fix 'InvalidOperationException: Scheme already exists: Bearer'
- How to fix System.InvalidOperationException: Scheme already exists: Identity.Application
- Add Thread ID to the Log File using Serilog
- Handling Exceptions in .NET Core API with Middleware
- InProcess Hosting in ASP.NET Core
- Limits on ThreadPool.SetMinThreads and SetMaxThreads
- Controlling DateTime Format in JSON Output with JsonSerializerOptions