<SCRIPT LANGUAGE="c#" RUNAT="server"> private int mnSessionMinutes = 2; private string msSessionCacheName="SessionTimeOut"; public void InsertSessionCacheItem(string sUserID) { try { if (this.GetSessionCacheItem(sUserID) != "") { return; } CacheItemRemovedCallback oRemove = new CacheItemRemovedCallback(this.SessionEnded); System.Web.HttpContext.Current.Cache.Insert(msSessionCacheName + sUserID,sUserID,null, DateTime.MaxValue, TimeSpan.FromMinutes(mnSessionMinutes),CacheItemPriority.High,oRemove); } catch (Exception e) { Response.Write(e.Message); } } public string GetSessionCacheItem(string sUserID) { string sRet=""; try { sRet = (string)System.Web.HttpContext.Current.Cache[msSessionCacheName + sUserID]; if (sRet == null) { sRet = ""; } } catch (Exception) { } return sRet; } public void SessionEnded(string key, object val, CacheItemRemovedReason r) { string sUserID=""; string sSessionTest=""; try { sSessionTest = Session["Test"].ToString(); } catch (Exception e) { sSessionTest = e.Message;} try { sUserID = (string)val; // Make sure your SMTP service has started in order for this to work System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage(); message.Body = "your session has ended for user : " + sUserID + " - Session String: " + sSessionTest; message.To = "your email goes here"; message.From = "info@eggheadcafe.com"; message.BodyFormat = System.Web.Mail.MailFormat.Text; message.Subject = "Session On End"; System.Web.Mail.SmtpMail.Send(message); message = null; } catch (Exception) { } } </SCRIPT>
<script Language="C#" runat="server"> protected string msUserID = "201"; protected string msSessionCacheValue=""; protected void Page_Load(object sender, EventArgs e) { ProcessPage(); } public void ProcessPage() { Session["Test"] = "test string to see if session variables still exist after app recycle."; ((global_asax)Context.ApplicationInstance).InsertSessionCacheItem(msUserID); // Test to access the cache and make sure it still exists msSessionCacheValue = ((global_asax)Context.ApplicationInstance).GetSessionCacheItem(msUserID); } </script> <HTML><BODY> <table border=0 align="center" cellpadding=2 cellspacing=2 width="70%"> <tr><td><br></td></tr> <tr><td><br></td></tr> <tr><td align="left">Close the browser and wait for email notification</td></tr> <tr><td><br></td></tr> <tr><td align="left"><a href=default.aspx target=_self>Redirect to this page to reaccess the cache</a></td></tr> <tr><td><br></td></tr> <tr><td align="left"><%= Session["Test"].ToString() %></td></tr> <tr><td><br></td></tr> <tr><td align="left"><%= msSessionCacheValue %></td></tr> </table> </body></html>