Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 357189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:10:56+00:00 2026-05-12T12:10:56+00:00

Which is the easiest and most unobstrusive way to keep an ASP.NET session alive

  • 0

Which is the easiest and most unobstrusive way to keep an ASP.NET session alive as long as the user has the browser window open? Is it timed AJAX calls? I want to prevent the following: sometimes users keep their window open for a long time, then enter stuff, and on submit nothing works anymore because the server side session expired. I don’t want to increase the timeout value for more than 10 min on the server as I want closed sessions (by closing the browser window) to time out fast.

Suggestions, code samples?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:10:56+00:00Added an answer on May 12, 2026 at 12:10 pm

    I use JQuery to perform a simple AJAX call to a dummy HTTP Handler that does nothing but keeping my Session alive:

    function setHeartbeat() {
        setTimeout("heartbeat()", 5*60*1000); // every 5 min
    }
    
    function heartbeat() {
        $.get(
            "/SessionHeartbeat.ashx",
            null,
            function(data) {
                //$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
                setHeartbeat();
            },
            "json"
        );
    }
    

    Session handler can be as simple as:

    public class SessionHeartbeatHttpHandler : IHttpHandler, IRequiresSessionState
    {
        public bool IsReusable { get { return false; } }
    
        public void ProcessRequest(HttpContext context)
        {
            context.Session["Heartbeat"] = DateTime.Now;
        }
    }
    

    The key is to add IRequiresSessionState, otherwise Session won’t be available (= null). The handler can of course also return a JSON serialized object if some data should be returned to the calling JavaScript.

    Made available through web.config:

    <httpHandlers>
        <add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeatHttpHandler"/>
    </httpHandlers>
    

    added from balexandre on August 14th, 2012

    I liked so much of this example, that I want to improve with the HTML/CSS and the beat part

    change this

    //$("#heartbeat").show().fadeOut(1000); // just a little "red flash" in the corner :)
    

    into

    beatHeart(2); // just a little "red flash" in the corner :)
    

    and add

    // beat the heart 
    // 'times' (int): nr of times to beat
    function beatHeart(times) {
        var interval = setInterval(function () {
            $(".heartbeat").fadeIn(500, function () {
                $(".heartbeat").fadeOut(500);
            });
        }, 1000); // beat every second
    
        // after n times, let's clear the interval (adding 100ms of safe gap)
        setTimeout(function () { clearInterval(interval); }, (1000 * times) + 100);
    }
    

    HTML and CSS

    <div class="heartbeat">&hearts;</div>
    
    /* HEARBEAT */
    .heartbeat {
        position: absolute;
        display: none;
        margin: 5px;
        color: red;
        right: 0;
        top: 0;
    }
    

    here is a live example for only the beating part: http://jsbin.com/ibagob/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the easiest way to convert a C++ std::string to another std::string, which has
Which is the easiest way to validate a file and to prevend changing it's
Hi I have a quick question, which is the easiest way to loop inside
Which static analysis tools for Java has easiest extension mechanism. I checked PMD But
What is the easiest way to create a Silverlight Button with custom Content which
Using Paypal, what is the easiest way to create a page on which a
I'm looking for the easiest and the most recommended way to upgrade mongodb version
Which is the easiest way I could get the file details / properties under
I'm looking for the easiest and most sensible way to allow this scenario to
I have a form which I need to validate the easiest and most safe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.