2
0

SessionInProcHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * Generate SessionID:s in a good (more random) way.
  15. */
  16. using System;
  17. using System.IO;
  18. using System.Collections;
  19. namespace System.Web.SessionState
  20. {
  21. // Container object, containing the current session state and when it was last accessed.
  22. internal class SessionContainer
  23. {
  24. private HttpSessionState _state;
  25. private DateTime last_access;
  26. public SessionContainer (HttpSessionState state)
  27. {
  28. _state = state;
  29. this.Touch ();
  30. }
  31. public void Touch ()
  32. {
  33. last_access = DateTime.Now;
  34. }
  35. public HttpSessionState SessionState {
  36. get {
  37. //Check if we should abandon it.
  38. if (_state != null && last_access.AddMinutes (_state.Timeout) < DateTime.Now)
  39. _state.Abandon ();
  40. return _state;
  41. }
  42. set {
  43. _state=value;
  44. this.Touch ();
  45. }
  46. }
  47. }
  48. internal class SessionInProcHandler : ISessionHandler
  49. {
  50. protected Hashtable _sessionTable;
  51. // The length of a session, in minutes. After this length, it's abandoned due to idle.
  52. const int SESSION_LIFETIME = 45;
  53. private SessionConfig config;
  54. public void Dispose ()
  55. {
  56. _sessionTable = null;
  57. }
  58. public void Init (HttpApplication context, SessionConfig config)
  59. {
  60. this.config = config;
  61. _sessionTable = (Hashtable) AppDomain.CurrentDomain.GetData (".MonoSessionInProc");
  62. if (_sessionTable == null)
  63. _sessionTable = new Hashtable();
  64. }
  65. public void UpdateHandler (HttpContext context, SessionStateModule module)
  66. {
  67. }
  68. //this is the code that actually do stuff.
  69. public bool UpdateContext (HttpContext context, SessionStateModule module)
  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 && !container.SessionState.IsAbandoned) {
  79. // Can we do this? It feels safe, but what do I know.
  80. container.SessionState.IsNewSession = false;
  81. // update the timestamp.
  82. container.Touch ();
  83. // Can we do this? It feels safe, but what do I know.
  84. context.SetSession (container.SessionState);
  85. return false; // and we're done
  86. } else if(container!=null) {
  87. _sessionTable.Remove (id);
  88. }
  89. }
  90. // else we create a new session.
  91. string sessionID = SessionId.Create (module.Rng);
  92. container = new SessionContainer (new HttpSessionState (sessionID, // unique identifier
  93. new SessionDictionary(), // dictionary
  94. new HttpStaticObjectsCollection(),
  95. SESSION_LIFETIME, //lifetime befor death.
  96. true, //new session
  97. false, // is cookieless
  98. SessionStateMode.InProc,
  99. false)); //readonly
  100. // puts it in the table.
  101. _sessionTable [sessionID]=container;
  102. AppDomain.CurrentDomain.SetData (".MonoSessionInProc", _sessionTable);
  103. // and returns it.
  104. context.SetSession (container.SessionState);
  105. context.Session.IsNewSession = true;
  106. // And we're done!
  107. return true;
  108. }
  109. }
  110. }