SessionInProcHandler.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  69. bool required, bool read_only, ref bool isNew)
  70. {
  71. SessionContainer container = null;
  72. string id = SessionId.Lookup (context.Request, config.CookieLess);
  73. //first we try to get the cookie.
  74. // if we have a cookie, we look it up in the table.
  75. if (id != null) {
  76. container = (SessionContainer) _sessionTable [id];
  77. // if we have a session, and it is not expired, set isNew to false and return it.
  78. if (container != null && container.SessionState != null &&
  79. !container.SessionState.IsAbandoned) {
  80. if (required)
  81. container.SessionState.SetNewSession (false);
  82. // update the timestamp.
  83. container.Touch ();
  84. if (required)
  85. return container.SessionState;
  86. return null;
  87. } else if (container!=null) {
  88. _sessionTable.Remove (id);
  89. }
  90. }
  91. if (!required)
  92. return null;
  93. // else we create a new session.
  94. string sessionID = SessionId.Create (module.Rng);
  95. HttpSessionState state = new HttpSessionState (sessionID, // unique identifier
  96. new SessionDictionary(), // dictionary
  97. HttpApplicationFactory.ApplicationState.SessionObjects,
  98. SESSION_LIFETIME, //lifetime before death.
  99. true, //new session
  100. false, // is cookieless
  101. SessionStateMode.InProc,
  102. read_only); //readonly
  103. container = new SessionContainer (state);
  104. _sessionTable [sessionID]=container;
  105. AppDomain.CurrentDomain.SetData (".MonoSessionInProc", _sessionTable);
  106. isNew = true;
  107. return container.SessionState;
  108. }
  109. }
  110. }