我有一个使用 JQuery 作为我的前端代码和 ASP.NET 作为我的后端 Web 服务的 Web 应用程序。
我设置了 web.config 设置 <sessionState timeout="1">。当用户登录时,Web 服务会使用用户名创建一个会话变量。
System.Web.HttpContext.Current.Session["UserID"] = user_id;
在我的网络服务中,我有一个检查变量是否仍然存在的函数。
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public string GetSessionUserID()
{
string user_id = "";
if (System.Web.HttpContext.Current.Session["UserID"] != null)
{
user_id = System.Web.HttpContext.Current.Session["UserID"].ToString();
}
return user_id;
}
我有一个 JS 函数,它调用调用GetSessionUserID().
$(document).ready(function () {
getSessionID();
setInterval(getSessionID, 3000);
function getSessionID() {
console.log("getSessionID");
$.ajax({
url: "photoapp.asmx/GetSessionUserID",
//data: ,
success: OnGetSessionIDSuccess,
error: OnGetSessionIDError
});
}
function OnGetSessionIDSuccess(data, status) {
console.log("Success OnGetSessionIDSuccess");
console.log(data);
var strUser = $(data).find("string").text();
console.log(strUser);
if (strUser == "") {
window.location = "login.html";
}
}
}
在文档准备功能中,我还调用setInterval()了它将每 3 秒检查一次会话。
测试时,getSessionID每 3 秒调用一次,但一分钟后,可以找到 getSessionID 用户变量。我希望在一分钟完成后将用户重定向回 login.html。为什么会话变量在一分钟后仍然存在?我对 ASP.Net 会话状态有什么不了解?如何解决这个问题?