Keeping a related ASP.NET application's session alive from another ASP.NET application

女生的网名这么多〃 提交于 2019-11-26 14:38:30

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/5642682/keeping-a-related-asp-net-applications-session-alive-from-another-asp-net-appli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!