Biff MaGriff
Biff MaGriff

Reputation: 8241

How would I prevent the session from expiring while using AJAX?

I have a .Net 3.5 website which uses windows authentication and expires the session using a meta tag on the prerender of my base masterpage class.

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    if (Response.ContentType == "text/html")
        this.Page.Header.Controls.Add(new LiteralControl(
            String.Format("<meta http-equiv='refresh' content='{0};url={1}'>",
            SessionLengthMinutes * 60, SessionExpireDestinationUrl)));
}

This works well for pages that do full post backs. However there are a few pages in my application where the user does a lot of work that is inside of an update panel. My company's policy is a timeout of 15 minutes. Which means, after 15 minutes of working inside of an update panel page, the user gets redirected to the application splash page.

Is there a way to reset or extend the meta tag on an async postback? Or perhaps a better way to accomplish this entirely?

Upvotes: 2

Views: 1969

Answers (3)

Barry Kaye
Barry Kaye

Reputation: 7759

A better way to accomplish this entirely would be to use javascript. This will prevent meta refresh related issues if your page is bookmarked.

In place of the page META REFRESH use this javascript:

<script type="text/javascript">
    var _timerID = setTimeout("window.location='splash-url'", 900000); //15 mins
</script>

When you make a request from the update panel use this javascript:

<script type="text/javascript">
    clearTimeout(_timerID);
    _timerID = setTimeout("window.location='splash-url'", 900000); //15 mins
</script>

Upvotes: 1

Shackles
Shackles

Reputation: 1264

You could use an AJAX request to keep the session alive as well. This will work as long as the user has opened your page in the browser. See http://808.dk/?code-ajax-session-keepalive

Upvotes: 0

BlackTigerX
BlackTigerX

Reputation: 6146

In the past I have used the WebMethod(EnableSession = true) attribute on the methods that respond to AJAX calls

Upvotes: 0

Related Questions