SessionInProcHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.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. const string COOKIE_NAME = "ASPSESSION"; // The name of the cookie.
  51. // The length of a session, in minutes. After this length, it's abandoned due to idle.
  52. const int SESSION_LIFETIME = 45;
  53. public void Dispose ()
  54. {
  55. _sessionTable = null;
  56. }
  57. public void Init (HttpApplication context)
  58. {
  59. _sessionTable = new Hashtable();
  60. }
  61. //this is the code that actually do stuff.
  62. public bool UpdateContext (HttpContext context)
  63. {
  64. SessionContainer container = null;
  65. //first we try to get the cookie.
  66. // if we have a cookie, we look it up in the table.
  67. if (context.Request.Cookies [COOKIE_NAME] != null) {
  68. container = (SessionContainer) _sessionTable [context.Request.Cookies [COOKIE_NAME].Value];
  69. // if we have a session, and it is not expired, set isNew to false and return it.
  70. if (container!=null && container.SessionState!=null && !container.SessionState.IsAbandoned) {
  71. // Can we do this? It feels safe, but what do I know.
  72. container.SessionState.IsNewSession = false;
  73. // update the timestamp.
  74. container.Touch ();
  75. // Can we do this? It feels safe, but what do I know.
  76. context.SetSession (container.SessionState);
  77. return false; // and we're done
  78. } else if(container!=null) {
  79. //A empty or expired session, lets kill it.
  80. _sessionTable[context.Request.Cookies[COOKIE_NAME]]=null;
  81. }
  82. }
  83. // else we create a new session.
  84. string sessionID = System.Guid.NewGuid ().ToString ();
  85. container = new SessionContainer (new HttpSessionState (sessionID, // unique identifier
  86. new SessionDictionary(), // dictionary
  87. new HttpStaticObjectsCollection(),
  88. SESSION_LIFETIME, //lifetime befor death.
  89. true, //new session
  90. false, // is cookieless
  91. SessionStateMode.InProc,
  92. false)); //readonly
  93. // puts it in the table.
  94. _sessionTable [sessionID]=container;
  95. // and returns it.
  96. context.SetSession (container.SessionState);
  97. context.Session.IsNewSession = true;
  98. // sets the session cookie. We're assuming that session scope is the default mode.
  99. context.Response.AppendCookie (new HttpCookie (COOKIE_NAME,sessionID));
  100. // And we're done!
  101. return true;
  102. }
  103. }
  104. }