5

I have 2 applications running on the same domain. The flow goes like so:

  1. Application 1
  2. Application 1 -> Application 2
  3. Application 2 -> Application 1

Application 1 is WebForms (asp.net framework 2.0), Application 2 is ASP.NET MVC 3 (framework 4.0)

While the user is on Application 2, I'd like to keep the session alive on Application 1.

While building Application 1, we built in a "KeepSessionAlive.ashx" handler that simply does Session("KeepSesssionAlive") = DateTime.Now() when requested, as described in this article. We did this because this is an assessment application and during some of the harder parts of test, a user might need a long time before they choose an answer. Here is the code:

Public Class KeepSessionAlive : Implements IHttpHandler, IRequiresSessionState  

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Session("KeepSessionAlive") = DateTime.Now                           
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property   

End Class

Then, I simply call this handler periodically within Application 1 using jQuery: $.post("KeepSessionAlive.ashx", null, function() { });

So, I figured I could call that same handler from Application 2 using $.ajax(), I even looked into using jsonp, but this doesn't seem to be working. I wrote code to log all the session variables from KeepSessionAlive.ashx to a file, and even to return stuff via jsonp response, and the data looked right.

However, doing a test in which I lingered in Application 2 long enough for Application 1's session to expire and then trying to do the transition from Application 1 -> Application 2, when I reach the return page in Application 1 I'm greeted with an System.NullReferenceException: Object reference not set to an instance of an object. error because I'm trying to reference one of the objects in Session. The only value in session is Session("KeepSessionAlive"). I assume this is because it created a new session, but if that's the case, why were my tests that logged the session values showing all of Application 1's session variables?

Are there any other methods I can use to keep Application 1's Session alive while the user is filling out the forms in Application 2?

JustinP8
  • 1,333
  • 1
  • 14
  • 32
  • Do the applications share an application pools or are they isolated? If they are isolated, does each app have a "KeepSessionAlive.ashx" or is the one in Application 1 the only one? – Chris Van Opstal Apr 12 '11 at 23:16
  • @Chris Pebble - They are on isolated application pools (Application 1 is a 2.0 framework app, Application 2 is 4.0). Only Application 1 has the "KeepSessionAlive.ashx" – JustinP8 Apr 13 '11 at 15:27

1 Answers1

6

Make a page on each site, thats reload a small image time to time.
Now instead of the image, you load a handler that return the image.

<img id="keepAliveIMG" width="1" height="1" src="/img/ui/spacer.gif?" alt="" /> 

<script language="javascript" type="text/javascript"> 
    var myImg = document.getElementById("keepAliveIMG");

    if (myImg){
        window.setInterval(function(){
              myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
            }, 6000);
    }   
</script> 

Then use an iframe inside your applications that load the other application page with the image reload. Or in general use an iframe, because with iframe you can keep the cookies updates from 2 diferent sites.

<iframe src="application2.aspx" width="0" height="0"></iframe>

Related : Reset session timeout without doing postback in ASP.Net

Community
  • 1
  • 1
Aristos
  • 64,863
  • 15
  • 114
  • 148
  • Thanks! This did work, but I wound up calling my KeepSessionAlive.ashx handler at an interval, rather than a .gif, from my KeepSessionAlive.aspx page (both on Application 1). Appliation 2 just appended the iframe to the body with the src set to to Application1/KeepSessionAlive.aspx. – JustinP8 Apr 13 '11 at 22:05
  • @JustinP8 yes the image calling is not 100% garandy that trigger the session, but the aspx is trigger it for sure. The trick on your solution is the iframe. – Aristos Apr 14 '11 at 07:44