Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Session.Abandon() asp.net Core

In Asp.net WebForms there is an event called Session_End() in global.asax, whenever session is timeout or you call Session.Abandon() this event handler is executed, i need to have similar kind of behavior in asp.net Core, is it possible?

like image 986
Xalisys Avatar asked Jun 24 '16 16:06

Xalisys


People also ask

What is session abandon ()?

The Abandon method destroys all the objects stored in a Session object and releases their resources. If you do not call the Abandon method explicitly, the server destroys these objects when the session times out.

What is the difference between session Clear () and session abandon () in asp net?

Clearing the session will not unset the session, it still exists with the same ID for the user but with the values simply cleared. Abandon will destroy the session completely, meaning that you need to begin a new session before you can store any more values in the session for that user.

Can we use session in .NET core?

Session state. Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. The session data is backed by a cache and considered ephemeral data.


2 Answers

You could clear the session by simply calling:

HttpContext.Session.Clear();

like image 144
Benader Avatar answered Oct 07 '22 23:10

Benader


The docs cover most of this. The session timeout is set like this:

services.AddSession(options =>
{
  options.IdleTimeout = TimeSpan.FromSeconds(10);
});

But since it exists is a cookie, the cookie also has an expiration date. So if IdleTimeout expires, the session expires. If the cookie expires, the session is gone. If the cookie is deleted, the session is gone.

Calling Session.Clear() removes the contents of the session, but keeps the session intact (aka, the cookie isn't deleted) as described in the source.

like image 42
Robert Paulsen Avatar answered Oct 07 '22 23:10

Robert Paulsen