保持ASP.NET会话打开/激活只要用户打开浏览器窗口,哪种最简单、最不引人注意的方式可以使ASP.NET会话保持活动?它是定时的Ajax调用吗?我想防止以下情况:有时用户将窗口打开很长时间,然后输入一些内容,而提交则不再有效,因为服务器端会话过期了。我不想在服务器上增加超过10分钟的超时值,因为我希望关闭会话(通过关闭浏览器窗口)来快速超时。建议代码样本?
3 回答
侃侃尔雅
TA贡献1801条经验 获得超16个赞
[HttpPost]public JsonResult KeepSessionAlive() { return new JsonResult {Data = "Success"};}
var keepSessionAlive = false;var keepSessionAliveUrl = null;function SetupSessionUpdater(actionUrl) { keepSessionAliveUrl = actionUrl; var container = $("#body"); container.mousemove(function () { keepSessionAlive = true; }); container.keydown(function () { keepSessionAlive = true; }); CheckToKeepSessionAlive();}function CheckToKeepSessionAlive() { setTimeout("KeepSessionAlive()", 300000);}function KeepSessionAlive() { if (keepSessionAlive && keepSessionAliveUrl != null) { $.ajax({ type: "POST", url: keepSessionAliveUrl, success: function () { keepSessionAlive = false; } }); } CheckToKeepSessionAlive();}
SetupSessionUpdater('/Home/KeepSessionAlive');
请注意!
明月笑刀无情
TA贡献1828条经验 获得超4个赞
public class KeepSessionAlive : IHttpHandler, IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); context.Response.Cache.SetNoStore(); context.Response.Cache.SetNoServerCaching(); } }
window.onload = function () { setInterval("KeepSessionAlive()", 60000)} function KeepSessionAlive() { url = "/KeepSessionAlive.ashx?"; var xmlHttp = new XMLHttpRequest(); xmlHttp.open("GET", url, true); xmlHttp.send(); }
- 3 回答
- 0 关注
- 404 浏览
添加回答
举报
0/150
提交
取消