3

I'm looking for a way to prevent the SessionState from refreshing when I make an AJAX post to a controller.

This controller returns a JSON true/false object if the SessionState has expired.

Public Class JsonController
    Inherits BaseController

    Function SessionCheck()
        If Session("Contact") Is Nothing Then
            Return Json(JsonResponseFactory.ErrorResponse("Invalid contact"))
        End If
        Return Json(JsonResponseFactory.SuccessResponse())
    End Function

End Class

Javascript:

function session_check_function() {
    if (!session_check_complete)
        return;
    session_check_complete = false;
    jQuery.post(
        base_url + 'json/SessionCheck',
        {},
        function (data) {
            if (!data.Success) {
                jQuery('#session-modal').modal('show');
            } else {
                jQuery('#session-modal').modal('hide');
            }
            session_check_complete = true;
        }
    ).fail(function () {
        jQuery('#session-modal').modal('show');
        session_check_complete = true;
    });
}

var session_check_complete = true;
var session_check = setInterval(function () { session_check_function(); }, 2500);

The problem is the session keeps refreshing everytime I do a POST to the controller, I don't want it to refresh.

tereško
  • 58,060
  • 25
  • 98
  • 150
Michael
  • 421
  • 2
  • 10
  • 23
  • Check out this question on keeping session alive: http://stackoverflow.com/questions/1270910/iframe-keep-alive-function-what-is-needed-to-reset-the-session-timeout – Andrew Lewis May 15 '13 at 14:06
  • @Kamyar the problem is the session keeps refreshing everytime I do a POST to the controller, I don't want it to refresh. – Michael May 15 '13 at 14:08

1 Answers1

4

You can use Sessionstate attribute on your controller to prevent it from refreshing session time out.

[SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)]
public class JsonController : BaseController  

Or in VB.Net:

<SessionState(System.Web.SessionState.SessionStateBehavior.Disabled)>
Public Class JsonController Inherits BaseController

Of course, this is set on the controller, so any other method in that controller does not refresh session timeout as well.

Update:
After digging a bit, I found this article which introduces a way to define session state behavior per action method. Another solution can be found at Disable Session state per-request in ASP.Net MVC

Community
  • 1
  • 1
Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • Thanks for the reply, I have tried something similar but unfortunately that disables the entire Session and I cannot check if a variable in the Session exists. – Michael May 15 '13 at 14:22
  • I could be wrong. I don't think you can disable session refresh and access it at the same time. – Kamyar May 15 '13 at 14:32
  • Okay well thanks for your help, if I use the SessionState attribute and I get the Session from System.Web.HttpContext.Session ? – Michael May 15 '13 at 14:49
  • Don't think that could make any difference. – Kamyar May 16 '13 at 08:36