使用 IIS 的 HttpApplicationState

HttpApplicationState using IIS

我的任务是创建一个基于浏览器的 POS 应用程序,该应用程序将在一台 PC 上使用 Windows IIS 服务器托管。当其他几台 PC 和平板电脑都连接到同一网络时,他们将通过在浏览器中输入托管它的 PC 的 IP 来访问该应用程序的功能。该应用程序将在 ASP.NET 中使用 C# 作为 ASPX 页面背后的代码进行编程。
我的问题如下:如果我使用 HttpApplicationState (var x = Application["testVariable"];) 来存储变量,它们对于连接到站点的每个设备来说是本地的还是所有设备都可以访问它们并且可以被覆盖?

示例:
iPad 以用户 A 身份连接并登录。我将变量 Application["userName"] 设置为字符串 "Nathangrad"。然后,PC 连接并以用户 B 身份登录。我将相同的变量设置为 "OtherUser"。现在,如果我在 iPad 上并调用 Application["userName"],我会得到 "Nathangrad" 还是 "OtherUser"

快速搜索 MSDN for the HttpApplicationState class 显示以下内容:

"Enables sharing of global information across multiple sessions and requests within an ASP.NET application."

"A single instance of an HttpApplicationState class is created the first time a client requests any URL resource from within a particular ASP.NET application virtual directory. A separate single instance is created for each ASP.NET application on a Web server. A reference to each instance is then exposed via the intrinsic Application object."

HttpApplicationState 是一个全局的东西,它对一个应用程序的所有用户都可用。来自 MSDN:

Enables sharing of global information across multiple sessions and requests within an ASP.NET application.

应用程序状态的典型用例是存储从不或很少更改且完全不依赖于用户的内容。

如果您确实需要覆盖值,但仍希望它们对整个应用程序全局可用,请考虑使用缓存 System.Runtime.Caching.MemoryCache 而不是应用程序状态。

要存储 user-specific 数据,您有两种选择:

  1. Session。同样,来自 MSDN:

ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session.

请注意,会话会在用户结束使用该网站后不久终止,并且存储的内容将变得不可用。

  1. 数据库持久化。如果您能够识别您的用户,则可以在数据库中存储必要的 user-specific 数据。此方法仅适用于经过身份验证的用户。