Home > Enterprise >  Are the values of Shared Properties outside of aspx pages Global to all sessions?
Are the values of Shared Properties outside of aspx pages Global to all sessions?

Time:01-13

Forgive me, it has been years and I am getting mixed results in my search. Using the VB code below in a traditional .NET forms application, will the value of ClientKey be global to the session or to the application domain?

Public Class AuthenticationClient

    Private Shared Property ClientKey As String
        Get
            Return HttpContext.Current.Session("ClientKey")
        End Get
        Set(value As String)
            HttpContext.Current.Session("ClientKey") = value
        End Set
    End Property
   
  'The function below is called only once on the FormsAuth login page for each successful login attempt
   Public Shared Sub SetClientKeyForSession(clientKeyForSession As String)
       ClientKey = clientKeyForSession 
   End Sub


   'This can be called for multiple contacts
   Public Shared Async Sub SaveSomething(userId As Integer)
       Await SomeDataAccess.SaveThisAsync(userId, ClientKey) '<- Not Me.ClientKey but is it the same session value set at login, I assume yes.
   End Sub

....
End Class

CodePudding user response:

Properties are just get and set methods. It's static fields that are shared by the app domain. Since you have no fields (only wrapping a session variable in get and set methods) everything is scoped to the session.

Note that automatic properties (e.g. Shared Property MyString As String in VB) actually create backing fields that you don't see. But that's not what you're doing here, so no backing field is created and no static data is used.

  •  Tags:  
  • Related