警告和扩展用户会话
本文关键字:会话 用户 扩展 警告 | 更新日期: 2023-09-27 18:09:52
我使用基本的身份验证为我的网站,有一个问题,当用户在一个表单上,需要一段时间来填写,他们的会话可能结束,并在点击"保存"的工作将消失,因为他们的会话不再有效。
我试了如下:
在web.config:
中设置会话超时时间为1分钟<system.web> <sessionState timeout="1"></sessionState>
将以下javascript添加到我的页面,它应该每30秒扩展一次会话:
AlertTimeOut = 30000; // Milliseconds, Alert Message
WarnTimeOut = 30000; // Warn Message before 30 sec of session out
setTimeout("WarnUser();", WarnTimeOut);
//setTimeout("AlertUser();", AlertTimeOut);
function AlertUser() {
alert('Your session has been expired, please save your data outside the system [copy the data in word document].');
}
function WarnUser() {
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10) {
minutes = "0" + minutes
}
if (hours > 11) {
var tempTime = hours + ":" + minutes + "PM"
} else {
var tempTime = hours + ":" + minutes + "AM"
}
var ans = confirm("Your login session is about to expire in 5 minutes.Do you want to extend it? - Date: " + tempTime);
if (ans) {
var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();
setTimeout("WarnUser();", WarnTimeOut);
}
else {
setTimeout("AlertUser();", AlertTimeOut);
}
}
这是我的main.aspx.cs页面的一个片段,在等待javascript弹出2次后,我刷新页面,是的,session["userid"]
是null
。我调试了javascript,它确实像它应该的那样击中了每一行代码。
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userid"] == null)
{
// the session is no longer valid, i guess the javascript didn't work :(
//redirect user to an error page
}
else
{
username = (string)Session["userid"];
// check user permission, and populate the form here
}
}
我在网上找到了一些使用类似代码的例子:
var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();
扩展会话,但我想我只是不明白它是如何工作的。这是一个图像,我应该有在我的aspx页面的某个地方?它是如何工作的?因为它不适合我
---- || ---- || ---- || ---- || ---- || ---- || ---- ||
更新:我还尝试了以下操作:
创建了一个refresh_session。只有一行代码的Aspx页面:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("success");
}
然后尝试POST到该页面以刷新会话(根据本文:http://naspinski.net/post/Automatically-refresh-your-users-Session-behind-the-scenes-using-jQuery-and-AspNet.aspx)
我更新的javascript看起来像:
AlertTimeOut = 30000; // Milliseconds, Alert Message
WarnTimeOut = 30000; // Warn Message before 30 sec of session out
setTimeout("WarnUser();", WarnTimeOut);
function TimedOut() {
$.post("refresh_session.aspx", null,
function (data) {
if (data == "success") {
setTimeout("TimedOut();", WarnTimeOut);
alert('refreshed');
}
else { alert('not refreshed'); }
}
);
}
function AlertUser() {
alert('Your session has been expired, please save your data outside the system [copy the data in word document].');
}
function WarnUser() {
var ans = confirm("Your login session is about to expire in 5 minutes.Do you want to extend it? ");
if (ans) {
var oImage = new Image;
oImage.src = "tempdummy.gif";
TimedOut() ;
setTimeout("WarnUser();", WarnTimeOut);
}
else {
setTimeout("AlertUser();", AlertTimeOut);
}
}
在frefresh_session的断点被击中,但是当我刷新我的页面时,它仍然是NULL在我的默认值。aspx :((((((
这个想法是,你从web服务器获取图像文件,每次用户点击确定在弹出窗口,所以它扩展会话。
你有一个图像"tempdummy.gif"在你的web服务器上吗?
我还是对代码片段有点困惑:
var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();
我不明白为什么他们在虚拟图像名称后面放一个随机数。在尝试获取图像时,您可能会遇到404。据我所知,导致404的http调用不会延长会话超时。
我会尝试从web服务器获取一个真正的(非常小)小图像,看看是否保持您的会话打开。