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.