0

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?

3
  • "I would like to redirect the user to the Home Screen just before the [session expires]" - are you sure a redirect is the best option here? What if the user is currently editing a form? Commented Mar 27, 2014 at 16:24
  • I was thinking of displaying a message just before it times out this is why I wanted to know if there is a something that fires before the session end. Commented Mar 27, 2014 at 16:29
  • You could use the example below of setInterval/setTimeout but set it like 30 to 60 seconds before what a timeout would actually be. You could prompt the user that their session is about to expire in that case. Commented Mar 27, 2014 at 16:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If you're going to do this, then why not just make the session timeout at the max possible time?
I will have a look at this. Thanks
By doing the session keep alive in the way above, we can also introduce some conditional logic in there - like the session will be renewed if the user has some unsaved edits on the page etc.,
0

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)

6 Comments

Wouldn't setTimeout be a better fit in this case?
They would both get the job done :-) In this scenario it would not matter really, since he wants to redirect when it hits. It will never run twice.
Thanks any other way doing it from the server.. rather than managing a variable as when a user changes page it will need to be reset ?
You could go about using SignalR, but this would again needs some js code at the client. The client actually does not have any direct connection to the server after the page has been loaded. Atleast not without using SignalR or something similar. You would just have to set the code posted above in the document.ready. You would not have to manage this in any way, if the timer hits, it redirects, otherwise it would not do anything.
As i have partial views in my application I'm assuming the document ready will need to be added to each page?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.