PDA

View Full Version : [TUT] Session Persist from Classic ASP to ASP.NET



Kyle Undefined
10-28-2009, 08:49 PM
Do you have a classic ASP application running along side with .NET and need to share sessions across? Well here is a simple solution to that problem. I recently ran into this problem while working on a clients site, it is written in classic ASP but I'm converting it to .NET. He requested a page that required the session variable from the login page on the classic side. After hours of research, here is what I have come up with and it works perfectly.

I have a common.asp page that I include into the pages that hold all of the code I use frequently. The easiest way I could find how to do this was make a form that was submitted by javascript. First, make a .NET page that the session variables will be sent to. Name it whatever you like, but I'm going to use "persist-session.aspx" Now, in the classic page make a sub and name it whatever you want. I'm naming mine PersistSessionToDotNet. Add a parameter to the sub that we will pass the destination url we want to navigate to after persisting to .NET.


Sub PersistSessionToDotNet(DestURL)
End Sub

Now, declare a variable with any name you want. I'm going to use the generic name "var"



Sub PersistSessionToDotNet(DestURL)
Dim var
End Sub

Now, do a Response.Write to begin the form.


Sub PersistSessionToDotNet(DestURL)
Dim var

Response.Write("<form name=""sessionpersist"" id=""sessionpersist"" action=""classic-asp-session-persist.aspx"" method=""post"">")
End Sub

Next, loop through the session variables that you are wanting to be persisted.


Sub PersistSessionToDotNet(DestURL)
Dim var

Response.Write("<form name=""sessionpersist"" id=""sessionpersist"" action=""classic-asp-session-persist.aspx"" method=""post"">")
For Each var In Session.Contents
Response.Write("<input type=""hidden"" name=""" & var & """ value=""" & Session.Contents(var) & """>")
Next
End Sub

Now, put in a hidden variable for the page you want to navigate to.


Sub PersistSessionToDotNet(DestURL)
Dim var

Response.Write("<form name=""sessionpersist"" id=""sessionpersist"" action=""classic-asp-session-persist.aspx"" method=""post"">")
For Each var In Session.Contents
Response.Write("<input type=""hidden"" name=""" & var & """ value=""" & Session.Contents(var) & """>")
Next
Response.Write("<input type=""hidden"" name=""destination"" value=""" & DestURL & """>")
End Sub

Then, we close the form and submit it through javascript and end the stream


Sub PersistSessionToDotNet(DestURL)
Dim var

Response.Write("<form name=""sessionpersist"" id=""sessionpersist"" action=""classic-asp-session-persist.aspx"" method=""post"">")
For Each var In Session.Contents
Response.Write("<input type=""hidden"" name=""" & var & """ value=""" & Session.Contents(var) & """>")
Next
Response.Write("<input type=""hidden"" name=""destination"" value=""" & DestURL & """>")
Response.Write("</form>")
Response.Write("<script language=""javascript"">sessionpersist.submit();</script>")
Response.End()
End Sub

Now that we have the classic ASP done you just need to make the .NET page retrieve it the session variables. We put this in the Page_Load. For this, we just loop through the variables then send the user on their way using the url we put as the parameter.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To Request.Form.Count - 1
Session(Request.Form.GetKey(i)) = Request.Form(i).ToString
Next
Response.Redirect(Session("Destination").ToString)
End Sub

And that's that. A short, sweet, easy way to pass session data from ASP to .NET

~Camo