Search code examples
asp.net-core-mvc

Session Timeout Notification on ASP.NET core


I want to get notified like "Your session will expire in 1 mins". I'm using ASP.NET core + React apps.

I've checked ASP.NET core documentation and stackoverflow, but no answer for ASP.NET core, just for ASP.NET. Please help me.


Solution

  • There is no event of session expiring. You have to implement it manually.

    function setupSessionTimeout(sessionExpirationTime) {
        var timeoutDuration = sessionExpirationTime * 60 * 1000;
        var timeoutId;
    
        function resetTimeout() {
            clearTimeout(timeoutId);
            timeoutId = setTimeout(notify, timeoutDuration); // Set timeout to trigger checkSessionExpired() after timeout duration
        }
    
        // Reset timeout on user activity (mousemove or keydown event)
        $(document).on('mousemove keydown', resetTimeout);
    
        // Initial setup
        resetTimeout(); // Set initial timeout
    }
    
    function notify() {
       alert("Session expired");
    }