Is there Session.Abandon() asp.net Core

2020-08-09 09:46发布

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?

2条回答
Evening l夕情丶
2楼-- · 2020-08-09 09:53

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.

查看更多
狗以群分
3楼-- · 2020-08-09 10:10

You could clear the session by simply calling:

HttpContext.Session.Clear();

查看更多
登录 后发表回答