Adalat  Khan

Adalat Khan

  • 625
  • 1.5k
  • 846.2k

Session time Countdown

Jul 30 2019 1:12 AM
I have MVC Core application in which each user has aspecified Session time. When a User login to the application the Session time count down counter starts and decrement time when the Session time reach to zero hour, zero minute, zero second then the application automatically sign out. I used the Timer control that calls a function after each second and the counter of seconds decremented. It is working perfectly and when the counter reach to zero the application sign out but the problem is that when a user login to the application and the counter starts then it works only when a user is in the Index View and when a user navigate to the menu and go to another View and then come back to the Main Index View the counter again starts from the begining suppose if a user login to the application and the counter starts from two hours Session time that is 2:00:00 and after some time if the user open another View from the application menu and then again come to the main View then the application do not maintain the counter but the counter again starts from two ours that is 2:00:00. I want such a counter that always keeps the time like static variable and even if a user go to another view from the application menu and then come back to the main menu the counter should not refresh but it should maintain the Session time. I used the timer and count down code in the _Layout.cshtm View. Following is my code:
  1. <script type="text/javascript">  
  2. @*Retrieve the seconds, minutes, and hours values from the Session which we have set in the Controller*@  
  3. var seconds = @HttpContextAccessor.HttpContext.Session.GetString("SessionSeconds");  
  4. var minutes = @HttpContextAccessor.HttpContext.Session.GetString("SessionMinutes");  
  5. var hours = @HttpContextAccessor.HttpContext.Session.GetString("SessionHours");  
  6. @*Store the seconds, minutes, and hours in the sessionStorage locally in client side*@  
  7. sessionStorage.SessionSeconds = seconds;  
  8. sessionStorage.SessionMinutes = minutes;  
  9. sessionStorage.SessionHours = hours;  
  10. var ts;  
  11. function CountDownSessionTime() {  
  12. if (Number(sessionStorage.SessionSeconds) > 0) {  
  13. sessionStorage.SessionSeconds = Number(sessionStorage.SessionSeconds) - 1;  
  14. }  
  15. else  
  16. if (Number(sessionStorage.SessionSeconds) == 0) {  
  17. sessionStorage.SessionSeconds = 60;  
  18. sessionStorage.SessionMinutes = Number(sessionStorage.SessionMinutes) - 1;  
  19. }  
  20. if (Number(sessionStorage.SessionMinutes) == 0 && Number(sessionStorage.SessionHours) > 0) {  
  21. sessionStorage.SessionHours = Number(sessionStorage.SessionHours) - 1;  
  22. minutes = 60;  
  23. }  
  24. document.getElementById("txtShowTime").innerHTML = Number(sessionStorage.SessionHours) + " : " + Number(sessionStorage.SessionMinutes) + " : " + Number(sessionStorage.SessionSeconds);  
  25. ts = setTimeout("CountDownSessionTime()", 1000);  
  26. if (Number(sessionStorage.SessionHours) == 0 && Number(sessionStorage.SessionMinutes) == 0 && Number(sessionStorage.SessionSeconds) == 0) {  
  27. clearTimeout(ts);  
  28. window.location.href = "@Url.Action("Login","Login")";  
  29. }  
  30. }  
  31. window.onload = CountDownSessionTime;  
  32. </script>  
I am displaying the Session time in the Footer section of the View. Following is my Code:
  1. <footer class="border-top footer text-muted">  
  2. <div class="container">  
  3. <label id="txtShowTime"></label>  
  4. </div>  
  5. </footer>  
Please give me an idea when the counter down operation starts and during this session a user navigate the application different views but the counter should not start from the begining but it should maintain the session time. Thanks

Answers (2)