Is there Session.Abandon() asp.net Core

2020-08-09 09:42发布

问题:

In Asp.net WebForms there is an event called Seesion_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?

回答1:

You could clear the session by simply calling:

HttpContext.Session.Clear();



回答2:

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.