SessionInProcHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // System.Web.SessionState.SessionInProcHandler
  3. //
  4. // Authors:
  5. // Stefan Görling ([email protected])
  6. //
  7. // (C) 2003 Stefan Görling
  8. //
  9. /*
  10. This is a rather lazy implementation, but it does the trick for me.
  11. TODO:
  12. * Remove abandoned sessions., preferably by a worker thread sleeping most of the time.
  13. * Increase session security, for example by using UserAgent i hashcode.
  14. */
  15. using System;
  16. using System.IO;
  17. using System.Collections;
  18. namespace System.Web.SessionState
  19. {
  20. // Container object, containing the current session state and when it was last accessed.
  21. internal class SessionContainer
  22. {
  23. private HttpSessionState _state;
  24. private DateTime last_access;
  25. public SessionContainer (HttpSessionState state)
  26. {
  27. _state = state;
  28. this.Touch ();
  29. }
  30. public void Touch ()
  31. {
  32. last_access = DateTime.Now;
  33. }
  34. public HttpSessionState SessionState {
  35. get {
  36. //Check if we should abandon it.
  37. if (_state != null && last_access.AddMinutes (_state.Timeout) < DateTime.Now)
  38. _state.Abandon ();
  39. return _state;
  40. }
  41. set {
  42. _state=value;
  43. this.Touch ();
  44. }
  45. }
  46. }
  47. internal class SessionInProcHandler : ISessionHandler
  48. {
  49. protected Hashtable _sessionTable;
  50. // The length of a session, in minutes. After this length, it's abandoned due to idle.
  51. const int SESSION_LIFETIME = 45;
  52. private SessionConfig config;
  53. public void Dispose ()
  54. {
  55. _sessionTable = null;
  56. }
  57. public void Init (HttpApplication context, SessionConfig config)
  58. {
  59. this.config = config;
  60. _sessionTable = (Hashtable) AppDomain.CurrentDomain.GetData (".MonoSessionInProc");
  61. if (_sessionTable == null)
  62. _sessionTable = new Hashtable();
  63. }
  64. public void UpdateHandler (HttpContext context, SessionStateModule module)
  65. {
  66. }
  67. //this is the code that actually do stuff.
  68. public bool UpdateContext (HttpContext context, SessionStateModule module)
  69. {
  70. SessionContainer container = null;
  71. string id = SessionId.Lookup (context.Request, config.CookieLess);
  72. //first we try to get the cookie.
  73. // if we have a cookie, we look it up in the table.
  74. if (id != null) {
  75. container = (SessionContainer) _sessionTable [id];
  76. // if we have a session, and it is not expired, set isNew to false and return it.
  77. if (container!=null && container.SessionState!=null && !container.SessionState.IsAbandoned) {
  78. // Can we do this? It feels safe, but what do I know.
  79. container.SessionState.IsNewSession = false;
  80. // update the timestamp.
  81. container.Touch ();
  82. // Can we do this? It feels safe, but what do I know.
  83. context.SetSession (container.SessionState);
  84. return false; // and we're done
  85. } else if(container!=null) {
  86. _sessionTable.Remove (id);
  87. }
  88. }
  89. // else we create a new session.
  90. string sessionID = SessionId.Create (module.Rng);
  91. container = new SessionContainer (new HttpSessionState (sessionID, // unique identifier
  92. new SessionDictionary(), // dictionary
  93. new HttpStaticObjectsCollection(),
  94. SESSION_LIFETIME, //lifetime befor death.
  95. true, //new session
  96. false, // is cookieless
  97. SessionStateMode.InProc,
  98. module.IsReadOnly)); //readonly
  99. // puts it in the table.
  100. _sessionTable [sessionID]=container;
  101. AppDomain.CurrentDomain.SetData (".MonoSessionInProc", _sessionTable);
  102. // and returns it.
  103. context.SetSession (container.SessionState);
  104. context.Session.IsNewSession = true;
  105. // And we're done!
  106. return true;
  107. }
  108. }
  109. }