SessionInProcHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Web.SessionState.SessionInProcHandler
  3. //
  4. // Authors:
  5. // Stefan Görling ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2003 Stefan Görling
  9. // Copyright (c) 2004 Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if !NET_2_0
  32. using System;
  33. using System.IO;
  34. using System.Collections;
  35. using System.Web.Caching;
  36. using System.Web.Configuration;
  37. namespace System.Web.SessionState
  38. {
  39. class SessionInProcHandler : ISessionHandler
  40. {
  41. const string INPROC_CACHE_PREFIX = "@@@InProc@";
  42. SessionConfig config;
  43. CacheItemRemovedCallback removedCB;
  44. public void Dispose () { }
  45. public void Init (SessionStateModule module, HttpApplication context,
  46. SessionConfig config)
  47. {
  48. removedCB = new CacheItemRemovedCallback (module.OnSessionRemoved);
  49. this.config = config;
  50. }
  51. public void UpdateHandler (HttpContext context, SessionStateModule module)
  52. {
  53. HttpSessionState session = context.Session;
  54. if (session == null || session.IsReadOnly || !session._abandoned)
  55. return;
  56. HttpRuntime.InternalCache.Remove ("@@@InProc@" + session.SessionID);
  57. }
  58. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  59. bool required, bool read_only, ref bool isNew)
  60. {
  61. if (!required)
  62. return null;
  63. Cache cache = HttpRuntime.InternalCache;
  64. HttpSessionState state = null;
  65. string id = SessionId.Lookup (context.Request, config.CookieLess);
  66. if (id != null) {
  67. state = (HttpSessionState) cache ["@@@InProc@" + id];
  68. if (state != null)
  69. return state;
  70. }
  71. // Create a new session.
  72. string sessionID = SessionId.Create ();
  73. state = new HttpSessionState (sessionID, // unique identifier
  74. new SessionDictionary(), // dictionary
  75. HttpApplicationFactory.ApplicationState.SessionObjects,
  76. config.Timeout, //lifetime before death.
  77. true, //new session
  78. false, // is cookieless
  79. SessionStateMode.InProc,
  80. read_only); //readonly
  81. TimeSpan timeout = new TimeSpan (0, config.Timeout, 0);
  82. cache.Insert (INPROC_CACHE_PREFIX + sessionID, state, null, Cache.NoAbsoluteExpiration,
  83. timeout, CacheItemPriority.AboveNormal, removedCB);
  84. isNew = true;
  85. return state;
  86. }
  87. public void Touch (string sessionId, int timeout)
  88. {
  89. Cache cache = HttpRuntime.InternalCache;
  90. if (cache == null || sessionId == null || sessionId.Length == 0)
  91. return;
  92. cache.SetItemTimeout (INPROC_CACHE_PREFIX + sessionId, Cache.NoAbsoluteExpiration, new TimeSpan (0, timeout, 0), false);
  93. }
  94. }
  95. }
  96. #endif