【发布时间】: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