SessionInProcHandler.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. SessionConfig config;
  42. CacheItemRemovedCallback removedCB;
  43. public void Dispose () { }
  44. public void Init (SessionStateModule module, HttpApplication context,
  45. SessionConfig config)
  46. {
  47. removedCB = new CacheItemRemovedCallback (module.OnSessionRemoved);
  48. this.config = config;
  49. }
  50. public void UpdateHandler (HttpContext context, SessionStateModule module)
  51. {
  52. HttpSessionState session = context.Session;
  53. if (session == null || session.IsReadOnly || !session._abandoned)
  54. return;
  55. HttpRuntime.Cache.Remove ("@@@InProc@" + session.SessionID);
  56. }
  57. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  58. bool required, bool read_only, ref bool isNew)
  59. {
  60. if (!required)
  61. return null;
  62. Cache cache = HttpRuntime.Cache;
  63. HttpSessionState state = null;
  64. string id = SessionId.Lookup (context.Request, config.CookieLess);
  65. if (id != null) {
  66. state = (HttpSessionState) cache ["@@@InProc@" + id];
  67. if (state != null)
  68. return state;
  69. }
  70. // Create a new session.
  71. string sessionID = SessionId.Create ();
  72. state = new HttpSessionState (sessionID, // unique identifier
  73. new SessionDictionary(), // dictionary
  74. HttpApplicationFactory.ApplicationState.SessionObjects,
  75. config.Timeout, //lifetime before death.
  76. true, //new session
  77. false, // is cookieless
  78. SessionStateMode.InProc,
  79. read_only); //readonly
  80. TimeSpan timeout = new TimeSpan (0, config.Timeout, 0);
  81. cache.InsertPrivate ("@@@InProc@" + sessionID, state, null, Cache.NoAbsoluteExpiration,
  82. timeout, CacheItemPriority.AboveNormal, removedCB);
  83. isNew = true;
  84. return state;
  85. }
  86. }
  87. }
  88. #endif