如何获得公共变量(一个模块),以不共享用户之间的价值变量、如何获得、模块、价值

2023-09-04 01:15:37 作者:疼惜ゞ

我在一个ASP.NET(VB)网站与Windows应用程序的工作/ Active Directory验证

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 reference variables, without having to instantiate a new object to access them on each page.

在这个模块,我有我正在使用多个页面在整个Web应用程序的一些公共变量。我最近意识到,该模块在这些公共变量的值的所有用户之间共享获得

Within that module, I have some Public variables that I am using in multiple pages throughout the web application. I've recently realized that the values for these public variables in the module get shared between all users.

目标: 我要为这些全局变量的值要具体到单个用户和所有会话之间不共享的,我不希望有实例化一个新的对象/类,它使用变量在每一页上。

THE GOAL: I want the value for these global variables to be specific to a single user and not shared between all sessions, and I do not want to have to instantiate a new object/class on every page that uses the variable.

美中不足的是: 我不想在客户端变量的值存储诸如一个cookie或会话。我想要的值被存储在服务器,但具体到每一个客户端/用户。

THE CATCH: I don't want to store the value in a client-side variable such as a cookie or session. I want the value to be stored on the SERVER but specific to each client/user.

我唯一能想到做的是建立一个全球性的集合/词典和存储与身份验证的用户名称的变量,但我需要有特定的功能来获取和设置的值。虽然这会工作,它需要在应用程序中的所有页面进行更新所有引用这些变量。

The only thing I can think to do is setup a global collection/dictionary and store the variables with the authenticated user names, but then I need to have specific functions to get and set the values. While this will work, it requires all the references to these variables on all pages in the application to be updated.

实例的问题: 下面code说明我是如何创建模块中的公共变量,以及如何价值正在从一个页面设置,并在另一个使用。我想继续以同样的方式使用此变量和共享它的页之间的值,但需要不会被用户之间共享的变量的值。

EXAMPLE OF THE PROBLEM: The below code shows how I am creating the public variable within the module and how the value is being set from one page and used on another. I'd like to continue to use this variable in the same way and share it's value between pages, but the value of the variable needs to NOT be shared between users.

- MODULE.VB -

-- MODULE.VB --

Public Module MyMod  
    Public myVariable as String = ""  
End Module  

- MAINPAGE.VB -

-- MAINPAGE.VB --

  Partial Class _Default  
        Sub Page_Load() Handles MyBase.Load()  
           myVariable = "HELLO WORLD"  
        End Sub  
    End Class  

- NEXTPAGE.VB -

-- NEXTPAGE.VB --

  Partial Class _Default  
        Sub Page_Load() Handles MyBase.Load()  
           Response.Write(myVariable)  
        End Sub  
    End Class  

有在此应用程序的页面,将需要手动更新,如果我用我的用户ID索引收集解决方案很多,所以我希望有一种方法简单地范围这些变量或不同的方式来关闭会话之间的共享。 在此先感谢!

There are a LOT of pages in this application that will need to be manually updated if I have to use my userID-indexed collection solution, so I'm hoping there is a way to simply scope these variables differently or a way to disable the sharing between sessions. Thanks in advance!

推荐答案

您没有说明是否不变量需要跨页面往返,还是他们只是每个页面的生命周期内使用。要坚持

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.

如果你想坚持的价值观,完成上述变化使得它更容易实现。然后,您可以打开基本页面上的成员变量到性能和嵌入用户的特定高速缓存和取的getter和setter。

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.

例如,而不是:

Public MyVariable As String = ""

您会碰到这样的:

Public Property MyVariable As String
    Get 
       Return GlobalMyVariableCache(UserNameKey)
    End Get
    Set (Value As String)
       GlobalMyVariableCache(UserNameKey) = Value
    End Set
End Property