How to prevent redirect to login page when session times out [ASP.NET]

Forums .NETHow to prevent redirect to login page when session times out [ASP.NET]
Staff asked 1 year ago

Even if the user is actively using the application, the session times out after 20 minutes and redirects to the login screen. However, I also want the user to be able to use the programme without being sent to the login page when the timeout occurs. Despite my extensive searching, I was unable to locate a solution to reset the timeout even when the user is actively using the application. Although increasing the timeout is a simple solution, it cannot be used for security concerns. I had no other options to get this done. Any assistance would be greatly appreciated in this. I appreciate it.

Answers (1)

Add Answer
krishna kukadiya Marked As Accepted
Staff answered 1 year ago

To prevent the automatic redirect to the login page when the session times out, you can implement a technique called “sliding expiration” or “sliding session timeout.” Sliding expiration extends the session timeout as long as the user remains active on the application, effectively resetting the timer.

Here’s an approach you can take to implement sliding expiration in your ASP.NET MVC application:

  1. In your web.config file, locate the <system.web> section and add or modify the following settings:
<system.web>
  <!-- Other settings... -->
  <sessionState timeout="20" cookieless="UseCookies" />
  <!-- Other settings... -->
</system.web>

In this example, the timeout attribute is set to 20 minutes, which matches your current session timeout configuration

  1. Create a custom action filter attribute that you can apply to controllers or actions where you want to enable sliding expiration. This attribute will update the session timeout whenever the user performs an action.
    using System;
    using System.Web.Mvc;
    
    public class SlidingExpirationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var session = filterContext.HttpContext.Session;
    
            // Extend session timeout by resetting the session expiry time
            session.Timeout = 20;
    
            base.OnActionExecuting(filterContext);
        }
    }

     

  2. Apply the SlidingExpirationAttribute to the controllers or actions where you want to enable sliding expiration.

    [SlidingExpiration]
    public class HomeController : Controller
    {
        // Controller actions...
    }

     

By applying the SlidingExpirationAttribute to specific controllers or actions, you ensure that the session timeout is extended whenever the user interacts with those areas of the application. This will prevent them from being redirected to the login page as long as they remain active.

Remember to apply the SlidingExpirationAttribute to the appropriate controllers or actions in your application based on your requirements.

Note: Sliding expiration provides a user-friendly experience by extending the session timeout as long as the user is active. However, it’s important to carefully consider the security implications of using sliding expiration, as it may introduce additional risks such as session hijacking. Make sure to follow best practices for session management and consider the specific security requirements of your application.

 

Subscribe

Select Categories