I was able to resolve this issue. Here's my approach.
In pageload
of default.aspx
I get the session time from web.config
file.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (!this.IsPostBack)
{
Session["Reset"] = true;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
}
And then using JavaScript function
I do a countdown and show a warning message when it hits 60 seconds.
<script type="text/javascript">
function SessionExpireAlert(timeout) {
var seconds = timeout / 1000;
document.getElementsByName("secondsIdle").innerHTML = seconds;
setInterval(function () {
seconds--;
document.getElementById("secondsIdle").innerHTML = seconds;
}, 1000);
setTimeout(function () {
//Show Popup before 20 seconds of timeout.
toastr.info('You will be logged out in: <span id="secondsIdle">' '</span> seconds.<br/>Click here to stay logged in.', "Warning",
{ timeOut: 0, 'position-class': 'toast-tontainer',onclick: renewSessionState}
}, timeout - 20 * 1000);
setTimeout(function () {
window.location = "logout.aspx";
}, timeout);
};
</script>
function renewSessionState() {
$.ajax({
url: "/Handlers/Ping.ashx",
method: 'POST'
});
}
Note* Ping.ashx
is just an empty handler.