【问题标题】:How would I prevent the session from expiring while using AJAX?使用 AJAX 时如何防止会话过期?
【发布时间】:2023-03-20 00:35:02
【问题描述】:

我有一个 .Net 3.5 网站,它使用 Windows 身份验证,并在我的基本母版页类的预呈现上使用元标记使会话过期。

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (Response.ContentType == "text/html")
        this.Page.Header.Controls.Add(new LiteralControl(
            String.Format("<meta http-equiv='refresh' content='{0};url={1}'>",
            SessionLengthMinutes * 60, SessionExpireDestinationUrl)));
}

这适用于进行完整回发的页面。但是,在我的应用程序中有几个页面,用户在更新面板内进行了大量工作。我公司的政策是暂停 15 分钟。这意味着,在更新面板页面内工作 15 分钟后,用户将被重定向到应用程序启动页面。

有没有办法在异步回发中重置或扩展元标记?或者也许是一个更好的方法来完全实现这一点?

【问题讨论】:

    标签: c# asp.net asp.net-ajax session-timeout meta-tags


    【解决方案1】:

    完全实现这一点的更好方法是使用 javascript。如果您的页面被加入书签,这将防止与元刷新相关的问题。

    使用此 javascript 代替 META REFRESH 页面:

    <script type="text/javascript">
        var _timerID = setTimeout("window.location='splash-url'", 900000); //15 mins
    </script>
    

    当您从更新面板发出请求时,请使用此 javascript:

    <script type="text/javascript">
        clearTimeout(_timerID);
        _timerID = setTimeout("window.location='splash-url'", 900000); //15 mins
    </script>
    

    【讨论】:

      【解决方案2】:

      过去我在响应 AJAX 调用的方法上使用了 WebMethod(EnableSession = true) 属性

      【讨论】:

        【解决方案3】:

        您也可以使用 AJAX 请求来保持会话处于活动状态。只要用户在浏览器中打开了您的页面,这将起作用。 见http://808.dk/?code-ajax-session-keepalive

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-05
          • 2021-12-28
          • 2010-11-30
          • 2015-03-10
          • 2014-07-16
          • 2015-09-15
          • 2012-06-10
          • 1970-01-01
          相关资源
          最近更新 更多