SessionInProcHandler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. using System;
  32. using System.IO;
  33. using System.Collections;
  34. using System.Web.Caching;
  35. using System.Web.Configuration;
  36. namespace System.Web.SessionState
  37. {
  38. class SessionInProcHandler : ISessionHandler
  39. {
  40. #if NET_2_0
  41. SessionStateSection config;
  42. #else
  43. SessionConfig config;
  44. #endif
  45. CacheItemRemovedCallback removedCB;
  46. public void Dispose () { }
  47. public void Init (SessionStateModule module, HttpApplication context,
  48. #if NET_2_0
  49. SessionStateSection config
  50. #else
  51. SessionConfig config
  52. #endif
  53. )
  54. {
  55. removedCB = new CacheItemRemovedCallback (module.OnSessionRemoved);
  56. this.config = config;
  57. }
  58. public void UpdateHandler (HttpContext context, SessionStateModule module)
  59. {
  60. HttpSessionState session = context.Session;
  61. if (session == null || session.IsReadOnly || !session._abandoned)
  62. return;
  63. HttpRuntime.Cache.Remove ("@@@InProc@" + session.SessionID);
  64. }
  65. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  66. bool required, bool read_only, ref bool isNew)
  67. {
  68. if (!required)
  69. return null;
  70. Cache cache = HttpRuntime.Cache;
  71. HttpSessionState state = null;
  72. string id = SessionId.Lookup (context.Request, config.CookieLess);
  73. if (id != null) {
  74. state = (HttpSessionState) cache ["@@@InProc@" + id];
  75. if (state != null)
  76. return state;
  77. }
  78. // Create a new session.
  79. string sessionID = SessionId.Create (module.Rng);
  80. state = new HttpSessionState (sessionID, // unique identifier
  81. new SessionDictionary(), // dictionary
  82. HttpApplicationFactory.ApplicationState.SessionObjects,
  83. #if NET_2_0
  84. (int)config.Timeout.TotalMinutes, // XXX is this right? we lose some precision here, but since the timeout is in minutes *anyway*...
  85. #else
  86. config.Timeout, //lifetime before death.
  87. #endif
  88. true, //new session
  89. false, // is cookieless
  90. SessionStateMode.InProc,
  91. read_only); //readonly
  92. #if NET_2_0
  93. TimeSpan timeout = TimeSpan.FromMinutes ((int)config.Timeout.TotalMinutes);
  94. #else
  95. TimeSpan timeout = new TimeSpan (0, config.Timeout, 0);
  96. #endif
  97. cache.InsertPrivate ("@@@InProc@" + sessionID, state, null, Cache.NoAbsoluteExpiration,
  98. timeout, CacheItemPriority.AboveNormal, removedCB);
  99. isNew = true;
  100. return state;
  101. }
  102. }
  103. }