如何清除 ASP.NET 中所有用户的 SQL 会话状态

发布于 2024-08-02 21:56:25 字数 263 浏览 3 评论 0原文

我使用 SQLServer SessionState 模式在 ASP.NET 应用程序中存储会话。它存储每次使用时序列化/反序列化的某些对象。

如果我对这些对象的结构的代码进行任何更改,并且启用新版本,则任何登录的用户都会收到错误,因为他们的会话对象(旧版本的)与新版本期望的结构不匹配,而反序列化。

有没有办法一次性清除数据库中的所有会话,以便所有活动会话过期并且用户被迫再次登录(因此所有会话对象都是从头开始创建的)?

或者……有没有其他办法可以解决这种情况?

I use SQLServer SessionState mode to store session in my ASP.NET application. It stores certain objects that are serialized/deserialized every time they are used.

If I make any changes in code of the structure of those objects and I put a new version live, any logged user will get an error, as their session objects (the old version ones) do not match the structure that the new version expects while deserializing.

Is there a way to clear all sessions at once in DB so that all active sessions expire and users are forced to log in again (and therefore all session objects are created from scratch)?

Or... Is there any other way to solve this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-08-09 21:56:25

您可以尝试使用SQL Server中的存储过程来清除所有会话:

CREATE PROCEDURE [dbo].[DeleteSessions]
AS
DELETE [ASPState].dbo.ASPStateTempSessions

RETURN 0

You may try using stored procedure in SQL Server to clear all the sessions:

CREATE PROCEDURE [dbo].[DeleteSessions]
AS
DELETE [ASPState].dbo.ASPStateTempSessions

RETURN 0
波浪屿的海角声 2024-08-09 21:56:25

当每个用户遇到无效的 Session 对象时,您可以为每个用户调用 Session.Abandon 或 Clear。

您还可以循环遍历每个用户的 Session 集合,并清除可能包含“旧”对象的键。也许您有一张登录票并且不想清除。

foreach (string key in Session.Keys)
{
  if (!key.Equals("login"))
  {
    Session.Remove(key);
  }
}

You can call Session.Abandon, or Clear for every user when they hit the invalid Session object.

You can also loop through the per-user Session collection, and clear the keys that can contain "old" objects. Maybe you have a login ticket and such that you don't want to clear.

foreach (string key in Session.Keys)
{
  if (!key.Equals("login"))
  {
    Session.Remove(key);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文