I have 2 applications running on the same domain. The flow goes like so:
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?
To use session in our Application, we need to add this package as a dependency in project. json file. The next step is to configure session in Startup class. We need to call "AddSession" method in ConfigureServices method of startup class.
In asp.net, It is very simple to detect session time out and redirect the user to login page or home page. All you have to do is, specify the redirection page in session_start event handler in Global. asax file as shown below. If the session has timed out, the user will be redirected to the login page.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With