【问题标题】:VB.NET Web Application - Unwanted users session sharingVB.NET Web 应用程序 - 不需要的用户会话共享
【发布时间】:2012-03-08 19:42:45
【问题描述】:

我对 vb.net 很陌生,因为我更像是一名 PHP 开发人员,但无论如何。我已经构建了一个 Web 应用程序,但我的用户似乎正在共享同一个会话,这是我不想要的,也不明白为什么。我从模块中的全局属性访问存储所有会话信息的对象,这可能是原因吗?

代码如下:

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

【问题讨论】:

  • 为什么使用静态类(模块)作为 Session 对象的存储库?静态意味着应用广泛。 mUserSession 也是静态的,因此所有用户共享同一个会话。
  • 啊...就像我说的,vb.net 的新手。我只是想要一种全局(非静态)方式来访问我的会话对象,而不是在整个地方重复相同的检查和访问。我的印象是模块只是全局可访问的函数存储空间,而不是静态的。

标签: asp.net vb.net session session-state


【解决方案1】:

为什么使用静态类(模块)作为 Session 对象的存储库?静态意味着应用范围广泛。 mUserSession 也是静态的,因此所有用户共享同一个会话。其实线条

mUserSession = value 

mUserSession = HttpContext.Current.Session("user")

在 getter/setter 中为所有用户覆盖它。

您可以将 Session 对象包装在您自己的类中,只是为了简化:

例如:

Public Class MySession
    ' private constructor
    Private Sub New()
    End Sub

    ' Gets the current session.
    Public Shared ReadOnly Property Current() As MySession
        Get
            Dim session As MySession = DirectCast(HttpContext.Current.Session("__MySession__"), MySession)
            If session Is Nothing Then
                session = New MySession()
                HttpContext.Current.Session("__MySession__") = session
            End If
            Return session
        End Get
    End Property

    ' **** add your session properties here, e.g like this:
    Public Property MyID() As Guid
        Get
            Return m_ID
        End Get
        Set(value As Guid)
            m_ID = value
        End Set
    End Property
    Private m_ID As Guid

    Public Property MyDate() As DateTime
        Get
            Return m_MyDate
        End Get
        Set(value As DateTime)
            m_MyDate = Value
        End Set
    End Property
    Private m_MyDate As DateTime

End Class

注意Current-property 也是共享/静态的,但不同之处在于我返回的是 HttpContext.Current.Session,而您返回的是单个/共享实例。

【讨论】:

  • 是的,我考虑过这一点,因为它类似于我在 PHP 中的做法。如果我知道我将拥有的模块的静态含义。我可以再问两个问题(如果算上这个,三个),为什么你选择使用 DirectCast 而不是 CType?我可以将 gUserSession 转换为返回 MySession.Current,这样我就不必重写所有代码,否则同样的问题会适用吗?
  • 1.) stackoverflow.com/a/3056582 也建议了解所涉及的实际类型,即使仅用于学习目的;) 2.) 只需使用我的 Current ,将其重命名为 gUserSession 并重命名MySessionUserSession.
  • @James:最后一点:仅对 Extensions 使用模块。在 99.999% 的其他情况下,您应该使用可能包含静态成员的(非静态)类。
猜你喜欢
  • 2013-04-10
  • 2012-04-05
  • 2017-03-19
  • 2019-09-27
  • 1970-01-01
  • 2011-03-07
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多