Session Management

I have the code below that creates a session everytime the website is loaded from the user's pc.  It works fine on the development machine, but when I upload the site to production.  It creates only one session from the first pc I opened the website, but when I use another pc to open the site it uses the same session that was created from the other pc.  I want the site to create a new session everytime the user loads it regardless from which pc.

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Private Shared fsCartID As String

    Public Shared ReadOnly Property ShoppingCartId() As String
        Get
            Return fsCartID
        End Get
    End Property

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim lobjContext As HttpContext = HttpContext.Current

        If ShoppingCartId Is Nothing Then
            fsCartID = Convert.ToString(lobjContext.Session("TestShoppingCartID"))

            If fsCartID = "" Then
                Dim lobjCartID As Guid = Guid.NewGuid

                lobjContext.Session("TestShoppingCartID") = lobjCartID.ToString
                fsCartID = lobjContext.Session("TestShoppingCartID").ToString
            End If
        End If
    End Sub
End Class
SillySoumareAsked:
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.

Bane83Commented:
You're a little mistaken.  It's not that it's using the same session, it's because you marked fsCartID as a Shared variable.  The class will only create this variable one time and use it for all users who access the site (with the same cartID every time).
0
SillySoumareAuthor Commented:
I changed it to the code below but I'm still getting the same session, no matter what pc the website is running.  Can you help me change the session when the site is reopened from another browser?

Imports System.Web.SessionState

Public Class Global_asax
    Inherits System.Web.HttpApplication

    Private fsCartID As String

    Public ReadOnly Property ShoppingCartId() As String
        Get
            Return fsCartID
        End Get
    End Property

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim lobjContext As HttpContext = HttpContext.Current

        If ShoppingCartId Is Nothing Then
            fsCartID = Convert.ToString(lobjContext.Session("TestShoppingCartID"))

            If fsCartID = "" Then
                Dim lobjCartID As Guid = Guid.NewGuid

                lobjContext.Session("TestShoppingCartID") = lobjCartID.ToString
                fsCartID = lobjContext.Session("TestShoppingCartID").ToString
            End If
        End If
    End Sub
End Class
0
Bane83Commented:
Also, try removing the ReadOnly from ShoppingCartID.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

SillySoumareAuthor Commented:
I removed the read only and it is still doing the same thing.  
0
Bane83Commented:
I was thinking about this last night before I went to sleep and I believe I figured out your problem.
It didn't hit me that you are doing this in the Global.asax which belongs to the application and is not created each time a user browses to the site.  This means that regardless of not using Shared, the cartID is being created and is static across all sessions because it belongs to the Global.asax.

Your fix is below.

Next, when you want to reference their cart ID you grab it directly from session:
Convert.ToString(Session("TestShoppingCartID"))
Imports System.Web.SessionState
 
Public Class Global_asax
    Inherits System.Web.HttpApplication
 
    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim lobjContext As HttpContext = HttpContext.Current
 
        If lobjContext.Session("TestShoppingCartID") Is Nothing Then
            lobjContext.Session("TestShoppingCartID") = System.GUID.NewGuid().ToString();
 
        End If
    End Sub
End Class

Open in new window

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
SillySoumareAuthor Commented:
You rock
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.