在ASP.NET应用程序中显示60秒之前的会话到期警报 [英] Show session expiry alert before 60 seconds in ASP.NET application

查看:47
本文介绍了在ASP.NET应用程序中显示60秒之前的会话到期警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在60秒之前显示会话到期提醒。它工作正常。



如果我们将会话时间设置为2分钟并且警报将在60秒之前到来。但是用户填写表单并且需要5到10分钟意味着用户在我的应用程序上处于活动状态,然后在这种情况下如何重置会话。
我当前代码会话中的
将仅在服务器端重置点击不是客户端活动。

请帮忙。



我尝试过:



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();
    }						

推荐答案

find(mpeTimeout)。show();
},超时 - 60 * 1000);
myTimer3 = setTimeout(function(){
window.location ='<%= ResolveUrl(〜/ CRM / Login.aspx)%>';
},超时) ;
};
函数ResetSession(){
//命中web方法以保留表单中的数据
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);
},
错误:函数(结果){
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;
配置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]
公共静态字符串ResetSession()
{
int TimeOut = 0;
try
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); //处理会话超时功能
HttpContext.Current.Session [Reset] =真;
配置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();
}
.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(); }


没有办法准确地做到这一点;但是,您可以做的是创建一个启用了Session的WebMethod,并且随时完成客户端活动,您可以使用jQuery的.ajax()调用来调用此WebMethod,这将重置会话服务器端而无需回发。



然后用JavaScript重置你的计时器。
There is no way to accurate do it; however, what you can do is create a WebMethod that has Session enabled and anytime client-side activities are done you can call this WebMethod using jQuery's .ajax() call which will reset the session server side without having to post back.

And then reset your timer in JavaScript.


这篇关于在ASP.NET应用程序中显示60秒之前的会话到期警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆