Page 1 of 1

Session Persist From Classic ASP to ASP.NET

#1 CamoDeveloper  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 20
  • View blog
  • Posts: 250
  • Joined: 12-June 09

Posted 17 July 2009 - 07:39 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

Is This A Good Question/Topic? 0
  • +

Replies To: Session Persist From Classic ASP to ASP.NET

#2 jkdunn5  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-February 12

Posted 15 February 2012 - 07:16 PM

@camo

I know this is an older post, but i was happy to find a simple method to pass session variables between a classic asp page and asp.net page. However i am having a problem with getting anything back to my asp.net page. Below i have posted both my asp page and my asp.net page. I tested the asp page by commenting out the form submit, and it writes out the session variables as expected. But i get nothing when my asp.net page tries to read the form variables. In my asp.net page i tried using your for iteration and the for each iteration shown in my example. Im sure at this point I just need Captain Obvious to show up and slap me, but any other things to try would be greatly appreciated.


My asp Page:
For Each var In Session.Contents 
       If IsArray(Session.Contents(var)) Then 
           ' Response.Write item & " = array, <br />" 
       Elseif IsObject(Session.Contents(var)) Then 
           ' Response.Write item & "= object, <br />" 
       Elseif Session.Contents(var) <> "" Then 
        Response.Write("<input type=""hidden"" name=""" & var & """ value=""" & Session.Contents(var) & """>")
      End If
    Next
    Response.Write("</form>")                                                                             
    Response.Write("<script language=""javascript"">redirectSearch.submit();</script>")



My asp.net Page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        
        Dim sw As StreamWriter = New StreamWriter("C:\\inetpub\\logs\\ISC.txt")
        For Each key As String In Request.Form
            sw.WriteLine(key & " = " & Request.Form(key))
            Session.Add(key, Request.Form(key))
        Next
        sw.Close()

End Sub



jkdunn
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1