我的.net应用程序中有一个Session类,当用户进行身份验证时(使用表单身份验证)我使用下面的类设置了一些其他会话数据
Friend NotInheritable Class MySession
Private Const EIDKey As String = "EID"
Private Sub New()
End Sub
Private Shared ReadOnly Property Session As HttpSessionState
Get
Return HttpContext.Current.Session
End Get
End Property
Public Shared ReadOnly Property Exists() As Boolean
Get
Return Not Session Is Nothing
End Get
End Property
Public Shared Property EID As Integer
Get
Return DirectCast(Session(EIDKey), Integer)
End Get
Set(ByVal value As Integer)
Session(EIDKey) = value
End Set
End Property
End Class
但是,在某些页面逻辑中,我会检查以直接返回登录页面
If IsNothing(Users.MySession.Exists) or Users.MySession.Exists = False Then Response.Redirect("/user/login")
If IsNothing(Users.MySession.EID) Then Response.Redirect("/user/login/")
但是在某些情况下,exists将返回true,并且EID会抛出一个null引用。我认为这可能发生在会话休息时?有更好的方法吗?