tau
tau

Reputation: 288

mvc Timeout Session_End

I have an MVC application and I would like to redirect the user to the Home Screen just before the Session_End event fires?

If I allow it to go to session end then it just terminates the session and the page is no longer active.

Any ideas on this?

Upvotes: 0

Views: 899

Answers (2)

DotCastle
DotCastle

Reputation: 11

If the idea behind your question is just not let the session terminate automatically, you can do the following

In your _Layout.cshtml, include a keep-alive pinging script. I am showing in Razor syntax here -

@{
   // Get the session timeout from configuration
   var sessionTimeout = (System.Web.Configuration.ConfigurationManager.GetSection("system.web/sessionState") as System.Web.Configuration.SessionStateSection).Timeout;
   // Ping interval can just a minute less that the session expiry
   var pingInterval = sessionTimeout.AddMinutes(-1.0);
}
<script type="text/javascript">
   window.setInterval(function () {
     $.ajax('@Url.Content("~")', { async: true, cache: false });
   }, @((int)pingInterval.TotalMilliseconds));
</script>

This script will ping your home page just one minute before the session expiry and thus kkeeping the session live.

Upvotes: 1

schei1
schei1

Reputation: 2487

Use setInterval() and window.location.replace(). The interval is in millisecond. So replace 3000 with with the interval you need.

setInterval(function(){
    window.location.replace(http://www.google.com)
}, 3000);

setTimeout will also be a viable option in this scenario (@Dismissle)

Upvotes: 0

Related Questions