如何在会话超时时执行代码?

How do I execute code upon session timeout?

我想在用户 Session 在我的网站上过期时清除一些临时数据。虽然大多数数据都存储在会话对象本身中,但我想与会话一起释放一些外部锁和保留。

我该把代码放在哪里?

如果您在 Global.asax.cs 中使用 inProc then you can do this add the following

protected void Session_End(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Session_End");
} 

否则您可能会想到一种不同的方法,即使预订本身真正临时化,对那些与会话匹配的预订本身设置超时,这可以随着用户的会话而延长。