2012-02-17 11 views
0

私はPHP開発者の方がはるかに多いので、vb.netは新しくなっています。私はWebアプリケーションを構築しましたが、私のユーザーは同じセッションを共有しているように見えますが、私はそれを望んでいないし、なぜ理解できません。私はすべてのセッション情報をモジュール内のグローバルプロパティから格納するオブジェクトにアクセスします。これが理由でしょうか?次のようにVB.NET 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 
+0

なぜあなたはSessionオブジェクトのリポジトリとして静的クラス(Module)を使用していますか?静的とは、アプリケーション全体を意味します。 'mUserSession'も暗黙的に静的なので、すべてのユーザーは同じセッションを共有します。 –

+0

ああ...私が言ったように、vb.netには初めてです。私はちょうど同じチェックを繰り返し、場所全体にアクセスするのではなく、私のセッションオブジェクトにアクセスするグローバルな(静的ではない)方法を望んでいました。私はモジュールが静的ではなくグローバルにアクセス可能な関数の記憶スペースであったという印象を受けました。 – James

答えて

2

は、なぜあなたは、あなたのSessionオブジェクトのリポジトリとして静的クラス(モジュール)を使用していますか?静的とはアプリケーション全体を意味します。 mUserSessionも暗黙的に静的であるため、すべてのユーザーが同じセッションを共有します。実際にゲッター/セッターの行

mUserSession = value 

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

は、すべてのユーザーのためにそれを上書きしています。

あなただけの簡略化のために独自のクラスで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を返していますが、あなたは単一の/共有インスタンスを返しています。

+0

はい私はこれを、PHPでどのようにしたのと同様に考えました。私が持っていたモジュールの静的な意味を知っていたかどうか。さらに2つの質問をしてもいいですか(これを数えれば3つです)、どうしてDirectCastをCTypeよりも使いたいのですか? gUserSessionをMySession.Currentに変換して、すべてのコードを書き直す必要はなく、同じ問題が当てはまるでしょうか? – James

+1

)1.)http://stackoverflow.com/a/3056582また、実際のタイプを知ることはお勧めです。学習目的であってもお勧めします;) 2.)私の 'Current'を使って' gUserSession' 'MySession'の名前を' UserSession'に変更します。 –

+0

@James:最後の1つの注意:[Extensions](http://msdn.microsoft.com/en-us/library/bb384936.aspx)のみのモジュールを使用してください。他の場合の99.999%では、静的メンバーを含む可能性のある(静的でない)クラスを使用する必要があります。 –

関連する問題