I am setting session time in web config. I also have a javascript for detecting session timeout and letting the user know.
I would like to be able to alert the user 30 sec or a min or more before session timeout with a popup and to give them the opportunity to extend it or cancel and just let it timeout. Can this be done? I have tried to use various methods on the internet but none seem to work. They all require that I download something and I don’t want to have to do that.
<script type="text/javascript">
var sessionTimeout = "<%= Session.Timeout %>";
function DisplaySessionTimeout() {
sessionTimeout = sessionTimeout - 1;
if (sessionTimeout >= 0)
window.setTimeout("DisplaySessionTimeout()", 60000);
else
alert("Your current Session is over due to inactivity.");
}
</script>
I am also checking for timeout in code behind and rediecting but thinking about just doing it in javascript.
protected void Page_Init(object sender, EventArgs e)
{
CheckSession();
}
private void CheckSession()
{
if (Session["ASP.NET_SessionId"] == null)
{
// Session.RemoveAll();
Response.Redirect("Home.aspx");
}
}
You have to essentialy have a timer at the same time limit as session; there is no way for the client to know exactly the time the session is at before expiring. That’ what I’ve done in an application.
We do a JQuery
$.get("keepalive.aspx")to make a request to an ASP.NET page, which accesses session and refreshes it, keeping the current session alive. You’d also then reset your client-side timer. This has worked well for us.See this post for more information.