[ASP/VB.NET][TUT] Session Persist From Classic ASP To ASP.NET

Discussion in 'Programming General' started by Kyle_Undefined, May 24, 2012.

[ASP/VB.NET][TUT] Session Persist From Classic ASP To ASP.NET
  1. Unread #1 - May 24, 2012 at 11:05 PM
  2. Kyle_Undefined
    Joined:
    Feb 9, 2012
    Posts:
    53
    Referrals:
    0
    Sythe Gold:
    0

    Kyle_Undefined Member

    [ASP/VB.NET][TUT] Session Persist From Classic ASP To ASP.NET

    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 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 "classic-asp-session-persist.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.

    Code:
    Sub PersistSessionToDotNet(DestURL)
    End Sub
    
    Now, declare a variable with any name you want. I'm going to use the generic name "var"

    Code:
    Sub PersistSessionToDotNet(DestURL)
        Dim var
    End Sub
    
    Now, do a Response.Write to begin the form.

    Code:
    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.

    Code:
    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.

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

    Code:
    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 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.

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

    I originally posted this over at DreamInCode, figured I'd share it here.
     
< [VB.NET][TUT] Class CountryIP (With C# example) | Anyone know eclipse >

Users viewing this thread
1 guest


 
 
Adblock breaks this site