Home > database >  Does an ajax request cause an asp.net session to be renew?
Does an ajax request cause an asp.net session to be renew?

Time:10-28

Have been working on detecting a timeout on an asp.net project. Have an ajax function that checks every 5 seconds to see if the session has expired. Without the ajax function checking, it actually expires after a minute, but if I keep the function on, it keeps sending me an "active" status. So I wonder, does my ajax function/request is keeping the session alive ?

Ajax function:

             function isSessionAlive(){
                await jQuery.ajax({
                type: "POST",
                url: 'coolpage.aspx/hello',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    console.info("status: ", response);
                },
                failure: function (response) {
                    console.info("status: ", response);
                },
                cache:false
            });}

Asp.net page method

    //[WebMethod(EnableSession = true)]
    [WebMethod]
    public static string hello()
    {
        //return (HttpContext.Current.Session["dummy"] == null) ? "expired" : "active";

        if (HttpContext.Current.Session != null)
        {
            if (HttpContext.Current.Session.IsNewSession)
            {
                string cookieHeader = HttpContext.Current.Request.Headers["Cookie"];
                if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    return "expired";
                }
            }
        }

        return "active";

        // return HttpContext.Current.User.Identity.IsAuthenticated ?  "active":"expired";

    }

CodePudding user response:

You have to use setInterval function in ajax.

setInterval(function(){
   $.get('coolpage.aspx/hello');
}, 300000); // 5mins

CodePudding user response:

If your session has expired, so destroy your session and return "expired". After calling ajax(document.ready), you have to check your session value using if and else.

  • Related