School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,137 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,826 people online right now. Registration is fast and FREE... Join Now!




Session Persist From Classic ASP to ASP.NET

 
Reply to this topicStart new topic

> Session Persist From Classic ASP to ASP.NET

CamoDeveloper
Group Icon



post 17 Jul, 2009 - 06:39 PM
Post #1


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

~Camo
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:08PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month