SessionInProcHandler.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. using System;
  12. using System.IO;
  13. using System.Collections;
  14. using System.Web.Caching;
  15. namespace System.Web.SessionState
  16. {
  17. class SessionInProcHandler : ISessionHandler
  18. {
  19. SessionConfig config;
  20. CacheItemRemovedCallback removedCB;
  21. public void Dispose () { }
  22. public void Init (SessionStateModule module, HttpApplication context, SessionConfig config)
  23. {
  24. removedCB = new CacheItemRemovedCallback (module.OnSessionRemoved);
  25. this.config = config;
  26. }
  27. public void UpdateHandler (HttpContext context, SessionStateModule module) { }
  28. public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
  29. bool required, bool read_only, ref bool isNew)
  30. {
  31. if (!required)
  32. return null;
  33. Cache cache = HttpRuntime.Cache;
  34. HttpSessionState state = null;
  35. string id = SessionId.Lookup (context.Request, config.CookieLess);
  36. if (id != null) {
  37. state = (HttpSessionState) cache ["@@@InProc@" + id];
  38. if (state != null)
  39. return state;
  40. }
  41. // Create a new session.
  42. string sessionID = SessionId.Create (module.Rng);
  43. state = new HttpSessionState (sessionID, // unique identifier
  44. new SessionDictionary(), // dictionary
  45. HttpApplicationFactory.ApplicationState.SessionObjects,
  46. config.Timeout, //lifetime before death.
  47. true, //new session
  48. false, // is cookieless
  49. SessionStateMode.InProc,
  50. read_only); //readonly
  51. TimeSpan timeout = new TimeSpan (0, config.Timeout, 0);
  52. cache.Insert ("@@@InProc@" + sessionID, state, null, DateTime.Now + timeout,
  53. timeout, CacheItemPriority.AboveNormal, removedCB);
  54. isNew = true;
  55. return state;
  56. }
  57. }
  58. }