0

I use basic authentication for my website and there is an issue when the user is on a form that takes a while to fill out, their session may end and upon clicking "save" the work will disappear because their session is no longer valid.

I tried the following:

set the session timeout to 1 minute in web.config:

<system.web>
<sessionState timeout="1"></sessionState>

added the following javascript to my page, which is supposed to extend the session every 30 seconds:

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);
    }
}

here is a snippet of my main.aspx.cs page, after waiting for the javascript pop up to come up 2 times, i refresh the page, and yes, the session["userid"] is null. I debugged the javascript and it does hit every line of code like it should.

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
         }
     }

i found quiet a few example online that use similar code to:

var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();

to extend session, but i guess i'm just not understanding how it works. is this an image i'm supposed to have somewhere eon my aspx page? how does it work? because it doesn't for me

---- || ---- || ---- || ---- || ---- || ---- || ---- ||

Update:

I also tried the following:

created a refresh_session.aspx page with only 1 line of code in it:

protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("success");
    }

I then try to POST to that page to refresh the seesion (as per this article:http://naspinski.net/post/Automatically-refresh-your-users-Session-behind-the-scenes-using-jQuery-and-AspNet.aspx)

my updated javascript looks like:

    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);
        }
    }

the breakpoint in frefresh_session is hit, but then when I refresh my page it is still NULL in my default.aspx :((((((

4

1 回答 1

0

这个想法是,每次用户在弹出窗口中单击“确定”时,您都会从网络服务器获取图像文件,从而扩展会话。

您的网络服务器上有图像“tempdummy.gif”吗?

我仍然对片段有点困惑:

var oImage = new Image;
oImage.src = "tempdummy.gif" + Math.random();

我不明白他们为什么在虚拟图像名称后面放一个随机数。在尝试获取图像时,您可能会遇到 404。据我所知,进行导致 404 的 http 调用不会延长会话超时。

我会尝试从网络服务器获取一个真实的(非常小的)小图像,看看这是否能让你的会话保持打开状态。

于 2013-04-17T13:55:10.547 回答