2
0

SessionInProcHandler.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public class SessionContainer
  22. {
  23. private HttpSessionState _state;
  24. private long _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.Millisecond;
  33. }
  34. public HttpSessionState SessionState {
  35. get {
  36. //Check if we should abandon it.
  37. if (_state != null &&
  38. (DateTime.Now.Millisecond - _last_access) > (_state.Timeout * 60 * 1000))
  39. _state.Abandon ();
  40. return _state;
  41. }
  42. set {
  43. _state=value;
  44. this.Touch ();
  45. }
  46. }
  47. }
  48. public class SessionInProcHandler : ISessionHandler
  49. {
  50. protected Hashtable _sessionTable;
  51. const string COOKIE_NAME = "ASPSESSION"; // The name of the cookie.
  52. // The length of a session, in minutes. After this length, it's abandoned due to idle.
  53. const int SESSION_LIFETIME = 45;
  54. public void Dispose ()
  55. {
  56. _sessionTable = null;
  57. }
  58. public void Init (HttpApplication context)
  59. {
  60. _sessionTable = new Hashtable();
  61. }
  62. //this is the code that actually do stuff.
  63. public void UpdateContext (HttpContext context)
  64. {
  65. SessionContainer container = null;
  66. //first we try to get the cookie.
  67. // if we have a cookie, we look it up in the table.
  68. if (context.Request.Cookies [COOKIE_NAME] != null) {
  69. container = (SessionContainer) _sessionTable [context.Request.Cookies [COOKIE_NAME].Value];
  70. // if we have a session, and it is not expired, set isNew to false and return it.
  71. if (container!=null && container.SessionState!=null && !container.SessionState.IsAbandoned) {
  72. // Can we do this? It feels safe, but what do I know.
  73. container.SessionState.IsNewSession = false;
  74. // update the timestamp.
  75. container.Touch ();
  76. // Can we do this? It feels safe, but what do I know.
  77. context.Session = container.SessionState;
  78. return; // and we're done
  79. } else if(container!=null) {
  80. //A empty or expired session, lets kill it.
  81. _sessionTable[context.Request.Cookies[COOKIE_NAME]]=null;
  82. }
  83. }
  84. // else we create a new session.
  85. string sessionID = System.Guid.NewGuid ().ToString ();
  86. container = new SessionContainer (new HttpSessionState (sessionID, // unique identifier
  87. new SessionDictionary(), // dictionary
  88. new HttpStaticObjectsCollection(),
  89. SESSION_LIFETIME, //lifetime befor death.
  90. true, //new session
  91. false, // is cookieless
  92. SessionStateMode.InProc,
  93. false)); //readonly
  94. // puts it in the table.
  95. _sessionTable [sessionID]=container;
  96. // and returns it.
  97. context.Session = container.SessionState;
  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. }
  102. }
  103. }