Kyle Undefined
2009-10-28 16:49:49
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.
Now, declare a variable with any name you want. I'm going to use the generic name "var"
Now, do a Response.Write to begin the form.
Next, loop through the session variables that you are wanting to be persisted.
Now, put in a hidden variable for the page you want to navigate to.
Then, we close the form and submit it through javascript and end the stream
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.
And that's that. A short, sweet, easy way to pass session data from ASP to .NET
~Camo
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 SubNow, declare a variable with any name you want. I'm going to use the generic name "var"
Sub PersistSessionToDotNet(DestURL)
Dim var
End SubNow, 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 SubNext, 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 SubNow, 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 SubThen, 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 SubNow 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 SubAnd that's that. A short, sweet, easy way to pass session data from ASP to .NET
~Camo