Skip to content

Instantly share code, notes, and snippets.

@bcalloway
Created March 28, 2012 15:32
Show Gist options
  • Save bcalloway/2227356 to your computer and use it in GitHub Desktop.
Save bcalloway/2227356 to your computer and use it in GitHub Desktop.
Session Timeout
@* Requires jQuery and jQuery UI *@
@if (this.Request.IsAuthenticated) {
int sessionDialogWait = 2 * 60 * 1000 - 60 * 500; // ms = 1.5 minutes
int sessionTimeout = 15 * 60 * 1000; // ms = 15 minutes
if (ViewData["sessionTimeout"] != null) {
sessionTimeout = ((int)ViewData["sessionTimeout"] * 60 - 120) * 1000;
}
<script type="text/javascript">
var logoutTimer = null;
var sessionTimer = null;
var sessionTimeout = Number('@sessionTimeout');
var sessionDialogWait = Number('@sessionDialogWait');
$(document).ready(function () {
$('#sessionEndDialog').dialog({
autoOpen: false,
bgiframe: true,
modal: true,
buttons: {
OK: function () {
$(this).dialog('close');
$.get('@Url.Action( "Index", "RentalHeaders" )', scheduleSessionPrompt, 'html');
},
Logout: logoutOnSessionExpires
}
}).ajaxStart(function () { scheduleSessionPrompt(); });
scheduleSessionPrompt();
});
function scheduleSessionPrompt() {
if (logoutTimer) clearTimeout(logoutTimer);
if (sessionTimer) clearTimeout(sessionTimer);
sessionTimer = setTimeout(sessionExpiring, sessionTimeout);
}
function sessionExpiring() {
logoutTimer = setTimeout(logoutOnSessionExpires, sessionDialogWait);
$('#sessionEndDialog').dialog('open');
}
function logoutOnSessionExpires() {
window.location.href = '@Url.Action( "Logout", "Authentications" )';
}
</script>
}
<div id="sessionEndDialog" title="Session Expiring" style="display: none;">
<p>Your session is about to expire. Click OK to renew your session or Logout to logout of the application.</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment