Attaching events to Session OnStart and OnEnd using HttpModules
-
22-08-2019 - |
Question
Does anyone know a clean way of adding events to Session's OnStart and OnEnd events using an HttpModule (without touching the Global.asax file)?
Solution
public void Init(HttpApplication app)
{
var ssm = app.Modules["Session"] as SessionStateModule;
ssm.Start += Foo;
ssm.End += Bar;
}
OTHER TIPS
Session OnStart behavior can be emulated by - in one of your HttpModule´s request events - checking if HttpContext.Current.Session.IsNewSession is set to true.
However there's one pitfall! If not a value is set in the Session object, the next request will have a positive value as IsNewSession. So once you possitively checked for IsNewSession you should set any value in a Session object.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow