Session Expire Notification

96
Views
1
Answers

Is there a way where an user is notified a minute before the session expires.

The ideal solution will be count down notification that will have an option to renew the session.

If the countdown timer expires without the user refreshing the page, I need to log them out.

edit | flag
William Patrick
Asked on: Jun 16, 2011 at 11:33PM

1 Answers

31
3
31

I think something like this will help..
In the controller..

public class Controller : BaseController {
       
public ActionResult TheAction() {
               
ViewData["SessionTimeout"] = Request.Session.Timout;
               
ViewData["SessionWillExpireOn"] = DateTime.Now.AddMinutes(Request.Session.Timeout);
               
return View(info);
       
}
}

In the View..

span>Your session will expire: %= ViewData["SessionWillExpireOn"].ToString() %>/span>
span id
="countDown" style="display:none>/span>
script type="
text/javascript">
        var sessionTimout = %= ViewData["
SessionTimeout"].ToString(); %>;              
        var approximateStart = new Date();
        var notifyAfter = new Date(approximateStart + (sessionTimout - 1)*60*1000);
        function startCountDown() {
                setTimout(function() {
                        var now = new Date();
                        document.getElementById('countDown').style.display = 'inline';
                        document.getElementById('countDown').innerHTML = "
Countdown: " + now.getMinutes();
                        if (now >= notifyAfter)
                                alert('About to expire...');
                }, 5000);
        }
        document.onload=startCountDown;
/script>
edit | flag
Gokul A
Answered on: Jun 16, 2011 at 11:38PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags