How to clear SQL session state for all users in ASP.NET

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

If I make any changes to the structure code of these objects, and I put the new version in real time, any registered user will receive an error, since their session objects (old versions) do not correspond to the structure in which the new version expects during deserialization.

Is there a way to clear all sessions at once in the database 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?

+3
source share
2 answers

You can call Session.Abandon or Clear for each user when they click on an invalid Session object.

You can also iterate over the collection of sessions for each user and clear keys that may contain "old" objects. Perhaps you have a ticket to enter and you do not want to clear.

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

You can try using the stored procedure on SQL Server to clear all sessions:

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

RETURN 0
+13
source

Source: https://habr.com/ru/post/1715879/


All Articles