Reset session timeout without doing postback in ASP.Net

前端 未结 3 1339
Happy的楠姐
Happy的楠姐 2020-12-06 23:47

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 timeou

3条回答
  •  长情又很酷
    2020-12-07 00:24

    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:

     
    
    

    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.

提交回复
热议问题