2
0

SessionInProcHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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, SessionConfig config)
  59. {
  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. //first we try to get the cookie.
  72. // if we have a cookie, we look it up in the table.
  73. if (context.Request.Cookies [COOKIE_NAME] != null) {
  74. container = (SessionContainer) _sessionTable [context.Request.Cookies [COOKIE_NAME].Value];
  75. // if we have a session, and it is not expired, set isNew to false and return it.
  76. if (container!=null && container.SessionState!=null && !container.SessionState.IsAbandoned) {
  77. // Can we do this? It feels safe, but what do I know.
  78. container.SessionState.IsNewSession = false;
  79. // update the timestamp.
  80. container.Touch ();
  81. // Can we do this? It feels safe, but what do I know.
  82. context.SetSession (container.SessionState);
  83. return false; // and we're done
  84. } else if(container!=null) {
  85. //A empty or expired session, lets kill it.
  86. _sessionTable[context.Request.Cookies[COOKIE_NAME]]=null;
  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. false)); //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. // sets the session cookie. We're assuming that session scope is the default mode.
  106. HttpCookie cookie = new HttpCookie (COOKIE_NAME,sessionID);
  107. cookie.Path = Path.GetDirectoryName (context.Request.Path);
  108. context.Response.AppendCookie (cookie);
  109. // And we're done!
  110. return true;
  111. }
  112. }
  113. }