Sharing session state between asp .net and asp -- advice needed pleas

Hi There,

I recently read an article that explained how to share session information between asp and asp .net. I tested it on my dev server and it seems to be working fine except that if I start another browser session it gives me access to the information I had from the previous session, for example if I have 2 browser windows open then it shows me the same information in both windows. I was just wondering if this is a design flaw or this is supposed to happen when accessing the website from the same machine using 2 different browser sessions.

article: http://www.asp101.com/articles/sidney/sharingsessionstate/default.asp

ASP >NET CODE -------------------

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data.SqlClient
Namespace pcf_solution_cs

    Public Class SessionTransfer
        Inherits System.Web.UI.Page

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim guidSave As String
            If Request.QueryString("dir") = "2asp" Then
                guidSave = AddSessionToDatabase()
                Response.Redirect("SessionTransfer.asp?dir=2asp&guid=" + guidSave + "&url=" + Server.UrlEncode(Request.QueryString("url")))
            Else
                GetSessionFromDatabase(Request.QueryString("guid"))
                ClearSessionFromDatabase(Request.QueryString("guid"))
                Response.Redirect(Request.QueryString("url"))
            End If
        End Sub

        Private Function AddSessionToDatabase() As String
            Dim strConnStr As String = (ConfigurationSettings.AppSettings("connectionString"))
            Dim con As SqlConnection = New SqlConnection(strConnStr)
            Dim cmd As SqlCommand = New SqlCommand
            con.Open()
            cmd.Connection = con
            Dim i As Integer = 0
            Dim strSql As String
            Dim guidTemp As String = GetGuid()
            While i < Session.Contents.Count
                strSql = "INSERT INTO ASPSessionState (GUID, SessionKey, SessionValue) " + "VALUES ('" + guidTemp + "', '" + Session.Contents.Keys(i).ToString + "', '" + Session.Contents(i).ToString + "')"
                cmd.CommandText = strSql
                cmd.ExecuteNonQuery()
                System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
            End While
            con.Close()
            cmd.Dispose()
            con.Dispose()
            Return guidTemp
        End Function

        Private Sub GetSessionFromDatabase(ByVal guidIn As String)
            Dim strConnStr As String = (ConfigurationSettings.AppSettings("connectionString"))
            Dim con As SqlConnection = New SqlConnection(strConnStr)
            Dim cmd As SqlCommand = New SqlCommand
            Dim dr As SqlDataReader
            con.Open()
            cmd.Connection = con
            Dim strSql As String
            Dim guidTemp As String = GetGuid()
            strSql = "SELECT * FROM ASPSessionState WHERE GUID = '" + guidIn + "'"
            cmd.CommandText = strSql
            dr = cmd.ExecuteReader
            While dr.Read
                Session(dr("SessionKey").ToString) = dr("SessionValue").ToString
            End While
            dr.Close()
            con.Close()
            cmd.Dispose()
            con.Dispose()
        End Sub

        Private Sub ClearSessionFromDatabase(ByVal guidIn As String)
            Dim strConnStr As String = (ConfigurationSettings.AppSettings("connectionString"))
            Dim con As SqlConnection = New SqlConnection(strConnStr)
            Dim cmd As SqlCommand = New SqlCommand
            con.Open()
            cmd.Connection = con
            Dim strSql As String
            strSql = "DELETE FROM ASPSessionState WHERE GUID = '" + guidIn + "'"
            cmd.CommandText = strSql
            cmd.ExecuteNonQuery()
            con.Close()
            cmd.Dispose()
            con.Dispose()
        End Sub

        Private Function GetGuid() As String
            Return System.Guid.NewGuid.ToString
        End Function

        Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
            InitializeComponent()
            MyBase.OnInit(e)
        End Sub

        Private Sub InitializeComponent()

        End Sub
    End Class
End Namespace

asp code

<%@ LANGUAGE="VBScript" %>
<%Option Explicit%>

<%
 dim DbConnectionStr
'***********************************
'Functions
'***********************************
 DbConnectionStr = "Provider=SQLOLEDB.1;" & _
                       "Password=pass;" & _
                       "Persist Security Info=True;" & _
                       "User ID=user;" & _
                       "Initial Catalog=dev;" & _
                       "Data Source=datasource;"


'Function returns a valid Globally Unique ID (GUID) for identifying a session.
Function GetGuid()
      Dim TypeLib, guid_temp
      
      Set TypeLib = Server.CreateObject("Scriptlet.TypeLib")
      
      'A true GUID contains a unicode null termination, that needs to be stripped to behave like
      '  a string.
      guid_temp = TypeLib.Guid
      GetGuid = Left(guid_temp, Len(guid_temp) - 2)
            
      Set TypeLib = Nothing
End Function

'This function adds all Session information to the database and returns the GUID used to
'  identify the Session information.
Function AddSessionToDatabase()
      'Declare Variables
      Dim con, cmd, strSql, guidTemp, i

      'Initialize Variables
      Set con = Server.CreateObject("ADODB.Connection")

'******************
      con.Open dbConnectionStr
'******************

      Set cmd = Server.CreateObject("ADODB.Command")
      cmd.ActiveConnection = con
      i = 1

      'Iterate through all Session variables and add them to the database with the
      '  same GUID as an identifier.
      guidTemp = GetGuid()
            
      Do While (i <= Session.Contents.Count)
            strSql = "INSERT INTO ASPSessionState (GUID, SessionKey, SessionValue) " + _
                  "VALUES ('" + guidTemp + "', '" + Session.Contents.Key(i) + "', '" + cstr(Session.Contents.Item(i)) + "')"
            cmd.CommandText = strSql
            cmd.Execute
            i = i + 1
            
            'response.Write Session.Contents.Key(i) & " " & Session.Contents.Item(i)
      Loop

      'Return the GUID used to identify the Session information
      AddSessionToDatabase = guidTemp

      'Clean up database objects
      con.Close
      Set cmd = Nothing
      Set con = Nothing
End Function

'This function retrieves the Session information identified by the parameter guidIn. The
'  resulting Session information is loaded into the Session object, it is not returned.
Sub GetSessionFromDatabase(guidIn)
      'Declare Variables
      Dim con, cmd, rs, strSql, guidTemp, i

      'Initialize Variables
      Set con = Server.CreateObject("ADODB.Connection")

'******************
      con.Open dbConnectionStr
'******************

      Set cmd = Server.CreateObject("ADODB.Command")
      cmd.ActiveConnection = con
      i = 1

      strSql = "SELECT * FROM ASPSessionState WHERE GUID = '" + guidIn + "'"
      Set rs = Server.CreateObject("ADODB.Recordset")
      rs.Open strSql, con

      While Not(rs.EOF)
            Session(rs("SessionKey")) = rs("SessionValue")
            rs.MoveNext
      Wend


      i = 1
      Do While (i <= Session.Contents.Count)
            Response.Write "Session(""" + Session.Contents.Key(i) + """) - " + Session.Contents.Item(i) + "<BR>"
            i = i + 1
      Loop
      
      'Clean up database objects
      rs.Close
      con.Close

      Set rs = Nothing
      Set cmd = Nothing
      Set con = Nothing
End Sub

'This performs cleanup of the Session information identified by the parameter guidIn. All Session
'  information in the database with the specified GUID is deleted.
Sub ClearSessionFromDatabase(guidIn)
      'Declare Variables
      Dim con, cmd, strSql

      'Initialize Variables
      Set con = Server.CreateObject("ADODB.Connection")

'******************
      con.Open dbConnectionStr
'******************

      Set cmd = Server.CreateObject("ADODB.Command")
      cmd.ActiveConnection = con
      
      'Remove all session variables from the database
      strSql = "DELETE FROM ASPSessionState WHERE GUID = '" + guidIn + "'"
      'Response.Write strSql + "<BR>"
      cmd.CommandText = strSql
      cmd.Execute

      'Clean up database objects
      con.Close
      Set cmd = Nothing
      Set con = Nothing
End Sub

'***********************************
'Main code execution
'***********************************

Dim guidSave

If Request.QueryString("dir") = "2aspx" Then
      'Store the session information in the database, and switch to ASP.NET
      guidSave = AddSessionToDatabase()
      Response.Redirect("SessionTransfer.aspx?dir=2aspx&guid=" + guidSave + "&url=" & Server.URLEncode(Request.QueryString("url")))
Else
      'Retrieve the session information and redirect to the specified URL
      Call GetSessionFromDatabase(Request.QueryString("guid"))

      'Clean up the database
      Call ClearSessionFromDatabase(Request.QueryString("guid"))
      Response.Redirect(Request.QueryString("url"))
End If
%>
LVL 5
bullroutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the_paabCommented:
When you create New Page in IE then it is created really only new window in the same session. New session is created when you run another IE process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bullroutAuthor Commented:
Hi paab,

Thanks for the answer. I did a little more testing and it seemed to work fine.

Sean
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.