How to get a public variable (in a Module) to NOT share value between users

后端 未结 2 1729
心在旅途
心在旅途 2021-01-21 19:23

I\'m working in an ASP.NET (VB) Web Application with Windows/Active Directory Authentication

I am using a module so that I can call public subroutines and functions, and

相关标签:
2条回答
  • 2021-01-21 19:57

    The problem you are coming across is a very common one in web programming. A Module's members are static - meaning there is one instance of them across the entire AppDomain of your application. Every user that accesses these will get the same object - you have already learned this.

    Your options are exactly what you described. You could possibly replace the public variable in your module with a property whose getter you write to access a user-specific field in a dictionary (please remember thread safety when writing this getter code).

    The much easier solution would be to use the Session. Session values are stored server-side and are user specific. The only thing that get's sent client side is the session key, and if you are using .Net authentication, this is likely already getting sent.

    Good luck,

    0 讨论(0)
  • 2021-01-21 20:02

    You didn't indicate whether or not the variables need to be persisted across page round trips or whether they are just used within each page's lifecycle.

    If they are not persisted across pages, then perhaps the easiest solution is to have all of your pages inherit from a based page class and then move the values from the module into the base page. This way you won't have to change any variable references, only page inheritance.

    If you do want to persist the values, completing the above changes makes it much easier to implement. You can then turn the member variables on the base page into properties and embed your user specific caching and fetching in the getter and setter.

    For example, instead of:

    Public MyVariable As String = ""
    

    You would have something like:

    Public Property MyVariable As String
        Get 
           Return GlobalMyVariableCache(UserNameKey)
        End Get
        Set (Value As String)
           GlobalMyVariableCache(UserNameKey) = Value
        End Set
    End Property
    
    0 讨论(0)
提交回复
热议问题