【问题标题】:Does an ajax request cause an asp.net session to be renew?ajax 请求是否会导致更新 asp.net 会话?
【发布时间】:2021-12-06 10:44:35
【问题描述】:

一直致力于检测 asp.net 项目的超时。有一个 ajax 函数,每 5 秒检查一次会话是否已过期。如果没有 ajax 函数检查,它实际上会在一分钟后过期,但如果我保持该函数打开,它会不断向我发送“活动”状态。所以我想知道,我的 ajax 函数/请求是否使会话保持活动状态?

Ajax 函数:

             function isSessionAlive(){
                await jQuery.ajax({
                type: "POST",
                url: 'coolpage.aspx/hello',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    console.info("status: ", response);
                },
                failure: function (response) {
                    console.info("status: ", response);
                },
                cache:false
            });}

Asp.net 页面方法

    //[WebMethod(EnableSession = true)]
    [WebMethod]
    public static string hello()
    {
        //return (HttpContext.Current.Session["dummy"] == null) ? "expired" : "active";

        if (HttpContext.Current.Session != null)
        {
            if (HttpContext.Current.Session.IsNewSession)
            {
                string cookieHeader = HttpContext.Current.Request.Headers["Cookie"];
                if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    return "expired";
                }
            }
        }

        return "active";

        // return HttpContext.Current.User.Identity.IsAuthenticated ?  "active":"expired";

    }

【问题讨论】:

  • 是的!你的情况好像是这样!!检查会话超时的 AJAX 请求实际上使您的会话保持活动状态。对于后端的每个请求,滑动会话到期窗口都会重置。您可以在 cookie 中保存会话到期时间并进行验证,而不是与服务器检查。
  • 谢谢,我也很怀疑。决定创建一个隐藏元素,并在检查任何变量的会话空值后在回发时更新它。 ...再次感谢。

标签: javascript c# asp.net ajax session


【解决方案1】:

如果您的会话已过期,请销毁您的会话并返回“过期”。调用 ajax(document.ready) 后,您必须使用 if 和 else 检查会话值。

【讨论】:

    【解决方案2】:

    你必须在 ajax 中使用 setInterval 函数。

    setInterval(function(){
       $.get('coolpage.aspx/hello');
    }, 300000); // 5mins
    

    【讨论】:

    • 谢谢 .... 是的,fn 实际上是从间隔调用的(未显示)。
    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2012-11-30
    • 1970-01-01
    • 2015-12-25
    • 2019-06-09
    • 1970-01-01
    相关资源
    最近更新 更多