How do I warn the user that their web session is about to time out?

后端 未结 1 1323
面向向阳花
面向向阳花 2020-12-30 18:20

I\'ve checked around for a solution but I don\'t seem to get any directed to asp.net mvc. Basically I\'m looking for a solution where a user is notified a minute before the

1条回答
  •  抹茶落季
    2020-12-30 19:09

    Something like this:

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

    and this:

    Your session will expire: <%= ViewData["SessionWillExpireOn"].ToString() %>
    
        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;
    
    

    Wrote the View in notepad, so please check the syntax. I also don't remember exactly the DateTime stuff in JS.

    Anyway you should see the idea.

    0 讨论(0)
提交回复
热议问题