使另一个 ASP.NET 应用程序的相关 ASP.NET 应用程序会话保持活动状态

发布于 2024-11-01 15:37:55 字数 1982 浏览 6 评论 0原文

我有 2 个应用程序在同一域上运行。流程如下:

  1. 应用程序 1
  2. 应用程序 1 -> 应用2
  3. 应用2 -> 应用程序 1

应用程序 1 是 WebForms (asp.net Framework 2.0),应用程序 2 是 ASP.NET MVC 3 (framework 4.0),

而用户位于应用程序 2 上,我希望在应用程序 1 上保持会话处于活动状态。

在构建应用程序 1 时,我们内置了一个“KeepSessionAlive.ashx”处理程序,该处理程序仅在请求时执行 Session("KeepSessionAlive") = DateTime.Now(),如 本文。我们这样做是因为这是一个评估应用程序,在测试的一些较难部分中,用户可能需要很长时间才能选择答案。代码如下:

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

然后,我只需使用 jQuery 在 Application 1 中定期调用此处理程序: $.post("KeepSessionAlive.ashx", null, function() { });

因此,我想我可以使用 $.ajax 从 Application 2 调用相同的处理程序(),我什至考虑过使用 jsonp,但这似乎不起作用。我编写了代码来将 KeepSessionAlive.ashx 中的所有会话变量记录到文件中,甚至通过 jsonp 响应返回内容,并且数据看起来正确。

但是,在进行测试时,我在应用程序 2 中停留了足够长的时间,以使应用程序 1 的会话过期,然后尝试从应用程序 1< 进行转换/strong>-> 应用程序 2,当我到达应用程序 1 中的返回页面时,我遇到了 System.NullReferenceException:未将对象引用设置为对象的实例。 错误,因为我试图引用 Session 中的对象之一。会话中的唯一值是 Session("KeepSessionAlive")。我认为这是因为它创建了一个新会话,但如果是这样,为什么我记录会话值的测试显示了所有应用程序 1 的会话变量?

当用户在应用程序 2 中填写表单时,是否可以使用其他方法来保持应用程序 1 的会话处于活动状态?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾性傲以野 2024-11-08 15:37:55

在每个站点上创建一个页面,即不时重新加载小图像。
现在,您加载一个返回图像的处理程序,而不是图像。

<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> 

然后在应用程序中使用 iframe,通过重新加载图像来加载其他应用程序页面。或者通常使用 iframe,因为使用 iframe,您可以保留来自 2 个不同站点的 cookie 更新。

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

相关: 重置会话超时而不在 ASP 中进行回发.Net

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文