Session Prompt for alerting user
Alerting the user when the session is expired and if you want to continue the session press yes and the same session will be extend, if you press no then the session will be cleared and redirect to page you want
Add the Html in the page you needed for session prompt:
<asp:HiddenField ID="hfTimeOut" runat="server" />
Add the following code in page_load of that page:
string reference = Page.ClientScript.GetCallbackEventReference(this, "", "clientReceiveServerData", "");
string callbackScript = "function clientCallServer()" +
"{" + reference + ";}";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientCallServer", callbackScript, true);
hdnTimeOut.Value = System.Configuration.ConfigurationManager.AppSettings["SessionWarning"];
Add the key in web.config:<add key="SessionWarning" value="5"/>
Script for session Timeout:
var i = 30;
function SetTime() {
if (i < 10)
$("#expiredTime").html("0" + i.toString() + " ");
else
$("#expiredTime").html(i.toString() + " ");
if (i > 0)
i--;
}
var isextend = false;
var isfirst = true;
var interval = 0;
var setTime = 0;
function TableHidden() {
$("#tbSessionExpired").css("display", "none");
$("#msgBoxBackGround").css("display", "none");
}
function TableShow() {
$("#msgBoxBackGround").css("display", "block");
$("#tbSessionExpired").css("display", "block");
}
function SetTimeextend() {
isextend = true;
$("#expiredTime").html("");
clearInterval(setTime);
clearInterval(interval);
clientCallServer();
TableHidden();
}
function SetTimeExpired() {
isextend = false;
window.location = "/Web/signout.aspx?id=1";
}
function TimeextendOrExpired() {
clearInterval(setTime);
clearInterval(interval);
setTime = setInterval("SetTime()", 1000);
interval = setInterval("ShowExpired()", 31000);
if (isfirst) {
isfirst = false;
TableShow();
i = 30;
}
else if (isextend) {
TableShow();
i = 30;
}
}
function ShowExpired() {
window.location = "/Web/signout.aspx?id=1";
}
function clientReceiveServerData(returnValue) { }
$(document).ready(function () {
if (window.location.pathname != "/Web/checkoutanon.aspx") {
var timeOut = $("input:hidden[id*='hfTimeOut']").val();
setInterval("TimeextendOrExpired()", timeOut * 60000);
}
});
The script will display the alert with two buttons yes and no when session is expired. if you click yes the session will be retained, if you press no the session will expire and redirected.Style for alert:
div.msgBox
{
padding: 15px;
position: fixed;
top: 35%;
left: 35%;
z-index: 10001;
display: none;
font-family: Verdana;
width: 34%;
min-height: 4%;
color: #00335e;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
-moz-box-shadow: 0px 0px 11px #000000;
-webkit-box-shadow: 0px 0px 11px #000000;
box-shadow: 0px 0px 11px #000000;
background-color: white;
}
div.msgBoxBackGround
{
top: 0;
left: 0;
position: absolute;
padding: 0;
display: none;
margin: 0;
width: 100%;
height: 100%;
background-color: #ccc;
opacity: 0.5;
z-index: 10000;
overflow:hidden;
}
.msgbox1
{
text-align: center;
color: #781D37;
font-size: 14px;
font-family: MuseoSlab700Regular;
}
.btnMsgbox
{
text-align: center;
padding-top: 10px;
}