开发者

How to execute server side code just before the asp.net session variable expires?

开发者 https://www.devze.com 2023-01-19 15:42 出处:网络
In my asp.net website I am creating a session upon user login and I would like to perform some operations in the database just before this s开发者_JAVA技巧ession will expire.I am having problem in det

In my asp.net website I am creating a session upon user login and I would like to perform some operations in the database just before this s开发者_JAVA技巧ession will expire.I am having problem in determining where should I write code and how will I know the session is going to expire.

I am not sure if 'session_end' event of 'Global.asax' suits my requirements as the session I want to check is created manually(not a browser instance).

Could someone please put me in the right direction?

Thanks.


This could be pretty tricky, namely because the Session_End method is only supported when the Session mode is set to InProc. What you could do, is use an IHttpModule that monitors an item stored in session, and fires an event when the Session expires. There is an example over on CodeProject (http://www.codeproject.com/KB/aspnet/SessionEndStatePersister.aspx), but its not without limitations, for instance it doesn't work in webfarm scenarios.

Using Munsifali's technique, you could do:

<httpModules>
 <add name="SessionEndModule" type="SessionTestWebApp.Components.SessionEndModule, SessionTestWebApp"/>
</httpModules>

And then wire up the module at the application start:

protected void Application_Start(object sender, EventArgs e)
{
  // In our sample application, we want to use the value of Session["UserEmail"] when our session ends
  SessionEndModule.SessionObjectKey = "UserEmail";

  // Wire up the static 'SessionEnd' event handler
  SessionEndModule.SessionEnd += new SessionEndEventHandler(SessionTimoutModule_SessionEnd);
}

private static void SessionTimoutModule_SessionEnd(object sender, SessionEndedEventArgs e)
{
   Debug.WriteLine("SessionTimoutModule_SessionEnd : SessionId : " + e.SessionId);

   // This will be the value in the session for the key specified in Application_Start
   // In this demonstration, we've set this to 'UserEmail', so it will be the value of Session["UserEmail"]
   object sessionObject = e.SessionObject;

   string val = (sessionObject == null) ? "[null]" : sessionObject.ToString();
   Debug.WriteLine("Returned value: " + val);
}

Then, when the Session starts, you can throw in some user data:

protected void Session_Start(object sender, EventArgs e)
{
   Debug.WriteLine("Session started: " + Session.SessionID);

   Session["UserId"] = new Random().Next(1, 100);
   Session["UserEmail"] = new Random().Next(100, 1000).ToString() + "@domain.com";

   Debug.WriteLine("UserId: " + Session["UserId"].ToString() + ", UserEmail: " + 
                 Session["UserEmail"].ToString());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消