Shafqat
Shafqat

Reputation: 1166

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?

Upvotes: 21

Views: 24493

Answers (2)

Benader
Benader

Reputation: 163

You could clear the session by simply calling:

HttpContext.Session.Clear();

Upvotes: 14

Robert Paulsen
Robert Paulsen

Reputation: 5171

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.

Upvotes: 6

Related Questions