Hi @Aypn CNN,
From what I understand, your current requirement is that when the user clicks on the page, the session must be restarted.
I've tested a simple example below. Assuming that the session time is 60 seconds, a session timeout notification will appear when it reaches 50 seconds.
When you click on the page, the session will refresh.
Demo
The Session Timeout has been set to one minute in Web.Config.
<system.web>
// ***
<sessionState timeout = "1"/>
</system.web>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$('html').on('click', function () {
ResetSession();
})
$(function () {
$("#dialog").dialog({
autoOpen: false,
modal: true,
title: "Session Expiring",
buttons: {
Ok: function () {
ResetSession();
},
Close: function () {
$(this).dialog('close');
}
}
});
});
function SessionExpireAlert(timeout) {
var seconds = timeout / 1000;
$("#secondsIdle").html(seconds);
$("#seconds").html(seconds);
setInterval(function () {
seconds--;
$("#secondsIdle").html(seconds);
$("#seconds").html(seconds);
}, 1000);
setTimeout(function () {
//Show Popup before 50 seconds of timeout.
$('#dialog').dialog('open');
}, timeout - 50 * 1000);
setTimeout(function () {
window.location = "MasterMenu.aspx";
}, timeout);
};
function ResetSession() {
//Redirect to refresh Session.
window.location = window.location.href;
};
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Session Idle: <span id="secondsIdle"></span> seconds.</h3>
<div id="dialog">
Your Session will expire in <span id="seconds"></span> seconds.<br />
Do you want to reset?
</div>
</form>
</body>
</html>
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Response.Cache.SetCacheability(HttpCacheability.NoCache)
If Not Me.IsPostBack Then
Session("Reset") = True
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.Config")
Dim section As SessionStateSection = DirectCast(config.GetSection("system.web/sessionState"), SessionStateSection)
Dim timeout As Integer = CInt(section.Timeout.TotalMinutes) * 1000 * 60
ClientScript.RegisterStartupScript(Me.GetType(), "SessionAlert", "SessionExpireAlert(" & timeout & ");", True)
End If
End Sub
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.