1

Is there any way to reset the session timeout without doing postback?

In this scenario I don't just want to hide the postback from user, I need to reset the timeout with out postback.

On my page I need to popup a dialog and inform the user that his session is timing out and if he wants to keep it alive he needs to click on a button. Now, when he clicks on that button I do not want to postback the page because it will cause issues with the data. (Not getting into the issues)


Based on the answers I have modified the code but it's still not working. docs.google.com/open?id=0B-Pl5DH2W9MvMDV6SUNiTXR0Z2M

Sev
  • 761
  • 4
  • 16
  • 29

3 Answers3

4

To refresh the session you need to make a call to something - I suggest to simple handler that just show an empty gif. Here is a simple code for that:

public class JustEmptyGif : IHttpHandler ,IRequiresSessionState 
{
    // 1x1 transparent GIF
    private readonly byte[] GifData = {
        0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
        0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
        0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
        0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
        0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
        0x02, 0x44, 0x01, 0x00, 0x3b
    };  

    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "image/gif";
        context.Response.Buffer = false;
        context.Response.OutputStream.Write(GifData, 0, GifData.Length);        
    }

    public bool IsReusable 
    {
        get {
            return false;
        }
    }
}

This code is just a handler, let say EmptyImage.ashx and notice that I have include the IRequiresSessionState that make it to call and update the session.

Now the only think that you have to do is to update a hidden image with some script, as:

<img id="keepAliveIMG" width="1" height="1" alt="" src="EmptyImage.ashx?" /> 
<script>
var myImg = document.getElementById("keepAliveIMG");
    myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
</script>

I have place the random number at the end to avoid to keep it on cache and force it to reload it again. No post back happens here, just a small image load.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I get the following error: Microsoft JScript runtime error: 'myImg' is null or not an object – Sev Dec 21 '12 at 22:49
  • I need to add a file and name it "EmptyImage.ashx?" ? – Sev Dec 21 '12 at 22:57
  • <%@ WebHandler Language="C#" Class="Handler" %> using System; using System.Web; public class JustEmptyGif : IHttpHandler, System.Web.SessionState.IRequiresSessionState {...} – Sev Dec 21 '12 at 23:06
  • it says Error 1 Could not create type 'Handler'. – Sev Dec 21 '12 at 23:07
  • @Sev Is very simple - on the Visual studio, right click on your project, add new item, select Generic Handler. – Aristos Dec 21 '12 at 23:10
  • OK I got it to run, now what should I do with the img tag to reset the session timeout? – Sev Dec 21 '12 at 23:16
  • @Sev Now you have include the img tag as I have place it there, and when a user click on a button, you run the script (eg place it on a function and just run it) – Aristos Dec 21 '12 at 23:19
  • I have a label that shows the session timeout every minute, after running your code it didn't reset – Sev Dec 21 '12 at 23:26
  • Let me share the project files in Google drive, what is your email address? – Sev Dec 21 '12 at 23:27
  • Then maybe you should come up with working code. You don't have to post if it's late and you need to sleep. Learn some manners. – Sev Dec 21 '12 at 23:33
  • Here is the code docs.google.com/open?id=0B-Pl5DH2W9MvMDV6SUNiTXR0Z2M – Sev Dec 21 '12 at 23:45
1

Only way I can think of is to do an ajax call to a dummy .net page like this:

function keepAlive()
{
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "KeepAlive.aspx", false );
xmlHttp.send( null );
}

and then call it like this

<input type=button onClick='keepAlive();' >
Kevin
  • 7,162
  • 11
  • 46
  • 70
  • That would depend on what you want to trigger this – Kevin Dec 21 '12 at 22:08
  • You could create a page that uses the session object (each http GET to it would refresh the session timeout value). Then in you client side code (if you had jQuery for instance) you could just do a $.get('/that/dummy/page.aspx') and it would tell the server that you are still interested in keeping your session alive. – Jason Sperske Dec 21 '12 at 22:13
  • I tried the keepalive example, but it does not reset the timer – Sev Dec 21 '12 at 23:36
  • @sev, is it hitting the keepalive.aspx? Did you step through it? – Kevin Dec 23 '12 at 02:08
0

There are a number of helpful suggestions here: How To Keep ASP.NET Session Alive.

Dov Miller
  • 1,958
  • 5
  • 34
  • 46