What is the best approach to handle session timeouts in asp.net

后端 未结 3 1513
庸人自扰
庸人自扰 2020-11-30 13:11

There are various ways to handle session timeouts, like \"meta refreshes\" javascript on load functions etc.

I would like something neat like: 5 minutes before timeo

3条回答
  •  情书的邮戳
    2020-11-30 13:30

    The only thing I can think of is to generate some script on the page that creates a client timer, so that when the page is received and rendered, it can show an alert X-minutes later (that is 5mins before expire).

    If you'd rather have the session just keep itself alive, you can do this with a generic handler (ASHX) that you periodically call via AJAX. This will help refresh the session and it should stay alive for as long as the AJAX calls continue.

    Example "keepalive.ASHX":

    <%@ WebHandler Language="C#" Class="keepalive" %>
    
    using System;
    
    public class keepalive : System.Web.IHttpHandler
    {       
        public void ProcessRequest (System.Web.HttpContext context) 
        {
            context.Response.ContentType = "text/json";
            var thisUser = System.Web.Security.Membership.GetUser();
    
            if (thisUser != null)
                context.Response.Write("[{\"User\": \"" + thisUser.UserName + "\"}]");
        }
    
        public bool IsReusable
        {
            get { return false; }
        }
    }
    

    And here's the script on the page to call it (with jQuery for simplicity):

    
    

提交回复
热议问题