MVC C# Session Time Out Popup

Challenge :

Develop an application which will alert the user the session is going to timeout in 30 seconds and allow the user to extend the session. If there is no activity session will be logged off.

Features:

  • ASP .net MVC
  • Can be integrated with any application
  • Minimal coding
  • Only applies if the user is logged in
  • Variables can be changed easily.
  • If the user is working on the browser without interacting with server still the code keeps the session alive by sending a heartbeat every minute.
  • heartbeat time can be controlled to avoid overloading server.
  • Heartbeat is lightweight to save bandwidth
  • No refreshing of the page is required
  • Adding to the layout file will protect all the pages.
  • Configurable timeout variables in web.config
  • Beep sound to alert the user when the popup is triggered.

 

Step 1 :

Create a partial view  _KeepAlive.cshtml inside Views/Shared folder and copy the following code.

https://github.com/haneefputtur/Protect_Timeout/blob/master/_KeepAlive.cshtml

Please edit these lines to match your configuration.

var sessoff = "/Account/Logoff"; //edit with your logoff url
var sessurl = "/home/KeepSessionAlive"; // edit line with your KeepSessionAlive URL of step 2

Step 2 :

Create an Action Method inside the controller and name it as KeepSessionAlive() and copy following code

[HttpPost]
public JsonResult KeepSessionAlive() {

return new JsonResult
{
Data = "Beat Generated"
};
}

Step 3:

 

add time out values in web config inside system.web.

 <sessionState timeout="4"></sessionState>

Step 4 :

Include the partial view inside Layout.cshtml

    @if (Request.IsAuthenticated)
    {
        @Html.Partial("~/Views/Shared/_KeepAlive.cshtml")
    }