I want to show session expiry alert before 60 seconds. It's working fine.
if we have set session time out 2 minutes and alert will come before 60 seconds. But user are fill a form and it will take 5 to 10 minutes means user are active on my application then in this case how to reset session .
in my current code session will be reset only on server side hit not client side activites.
Please help.
What I have tried:
Inside web config--
<sessionState timeout="5"/>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" EnablePartialRendering="true" ScriptMode="Release" AsyncPostBackTimeout="150">
</asp:ScriptManager>
<asp:LinkButton ID="lnkFake" runat="server" />
<asp:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout" runat="server" PopupControlID="pnlPopup"
TargetControlID="lnkFake" OkControlID="btnYes" BackgroundCssClass="modalBackground"
OnOkScript="ResetSession()">
</asp:ModalPopupExtender>
<asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none">
<div class="header">
Session Expiring!
</div>
<div class="body">
Your Session will expire in <span id="seconds"></span> seconds.<br />
Do you want to reset?
</div>
<div class="btnSection" align="right">
<asp:Button ID="btnYes" runat="server" Text="OK" CssClass="yes" />
</div>
</asp:Panel>
<script type="text/javascript">
var myTimer;
var myTimer2;
var myTimer3;
function SessionExpireAlert(timeout) {
var seconds = timeout / 1000;
document.getElementsByName("seconds").innerHTML = seconds;
myTimer = setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
}, 1000);
myTimer2=setTimeout(function () {
//Show Popup before 20 seconds of timeout.
$find("mpeTimeout").show();
}, timeout - 60 * 1000);
myTimer3 = setTimeout(function () {
window.location = '<%=ResolveUrl("~/CRM/Login.aspx") %>';
}, timeout);
};
function ResetSession() {
// hit web method to retain data in form
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: '<%=ResolveUrl("~/CRM/Login.aspx/ResetSession") %>',
dataType: "json",
success: function (data) {
clearInterval(myTimer);
clearInterval(myTimer2);
clearInterval(myTimer3);
SessionExpireAlert(data.d);
},
error: function (result) {
alert("Error");
}
});
}
</script>
============Login.aspx.cs----
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (!Page.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;
ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
}
[WebMethod]
public static string ResetSession()
{
int TimeOut = 0;
try
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);// To handle Session Timeout Functionality
HttpContext.Current.Session["Reset"] = true;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
TimeOut = (int)section.Timeout.TotalMinutes * 1000 * 60;
//ScriptManager.RegisterStartupScript(this, typeof(string), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
catch (Exception ex)
{
}
return TimeOut.ToString();
}