I’m quite new to vb.net as I’m much more of a PHP developer but anyway. I’ve built a web application but my users seem to be sharing the same session which I do not want and can’t understand why. I access the object which I store all session information in from a global property in a module, could this be the reason?
The code is as follows:
Module SiteWide
Private mUserSession As New MyLib.User.UserSession
Public Property gUserSession() As MyLib.User.UserSession
Get
If Not HttpContext.Current Is Nothing AndAlso Not HttpContext.Current.Session Is Nothing Then
If Not HttpContext.Current.Session("user") Is Nothing Then
mUserSession = HttpContext.Current.Session("user")
End If
End If
Return mUserSession
End Get
Set(ByVal value As MyLib.User.UserSession)
mUserSession = value
If Not HttpContext.Current Is Nothing AndAlso Not HttpContext.Current.Session Is Nothing Then
HttpContext.Current.Session("user") = value
End If
End Set
End Property
End Module
Why are you using a static class(Module) as repository for your Session objects? Static means application wide.
mUserSessionis also static implicitely, therefore all users share the same Session. Actually the linesand
in the getter/setter are overwriting it for all users.
You could wrap the Session object in your own class just for simplification:
For example:
Note: The
Current-property is also shared/static, but the difference is that i’m returningHttpContext.Current.Sessionwhereas you are returning a single/shared instance.