SessionStateModule.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Web.SessionState.SesionStateModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Stefan Görling ([email protected])
  7. // Jackson Harper ([email protected])
  8. //
  9. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  10. // (C) 2003 Stefan Görling (http://www.gorling.se)
  11. using System.Web;
  12. using System.Web.Caching;
  13. using System.Web.Util;
  14. using System.Security.Cryptography;
  15. namespace System.Web.SessionState
  16. {
  17. public sealed class SessionStateModule : IHttpModule
  18. {
  19. internal static readonly string CookieName = "ASPSESSION";
  20. internal static readonly string HeaderName = "AspFilterSessionId";
  21. static SessionConfig config;
  22. static Type handlerType;
  23. ISessionHandler handler;
  24. private RandomNumberGenerator rng;
  25. public SessionStateModule ()
  26. {
  27. rng = new RNGCryptoServiceProvider ();
  28. }
  29. internal RandomNumberGenerator Rng {
  30. get { return rng; }
  31. }
  32. public void Dispose ()
  33. {
  34. if (handler!=null)
  35. handler.Dispose();
  36. }
  37. public void Init (HttpApplication app)
  38. {
  39. if (config == null) {
  40. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  41. if (config == null)
  42. config = new SessionConfig (null);
  43. if (config.Mode == SessionStateMode.StateServer)
  44. handlerType = typeof (SessionStateServerHandler);
  45. if (config.Mode == SessionStateMode.SQLServer)
  46. handlerType = typeof (SessionSQLServerHandler);
  47. if (config.Mode == SessionStateMode.InProc)
  48. handlerType = typeof (SessionInProcHandler);
  49. if (config.Mode == SessionStateMode.Off)
  50. return;
  51. }
  52. if (config.CookieLess)
  53. app.BeginRequest += new EventHandler (OnBeginRequest);
  54. app.AddOnAcquireRequestStateAsync (
  55. new BeginEventHandler (OnBeginAcquireState),
  56. new EndEventHandler (OnEndAcquireState));
  57. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  58. app.EndRequest += new EventHandler (OnEndRequest);
  59. if (handlerType != null && handler == null) {
  60. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  61. handler.Init (this, app, config); //initialize
  62. }
  63. }
  64. void OnBeginRequest (object o, EventArgs args)
  65. {
  66. HttpApplication application = (HttpApplication) o;
  67. HttpContext context = application.Context;
  68. string base_path = context.Request.BaseVirtualDir;
  69. string id = UrlUtils.GetSessionId (base_path);
  70. if (id == null)
  71. return;
  72. context.Request.SetFilePath (UrlUtils.RemoveSessionId (base_path,
  73. context.Request.FilePath));
  74. context.Request.SetHeader (HeaderName, id);
  75. }
  76. void OnReleaseRequestState (object o, EventArgs args)
  77. {
  78. if (handler == null)
  79. return;
  80. HttpApplication application = (HttpApplication) o;
  81. HttpContext context = application.Context;
  82. handler.UpdateHandler (context, this);
  83. }
  84. void OnEndRequest (object o, EventArgs args)
  85. {
  86. }
  87. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  88. {
  89. HttpApplication application = (HttpApplication) o;
  90. HttpContext context = application.Context;
  91. bool required = (context.Handler is IRequiresSessionState);
  92. bool read_only = (context.Handler is IReadOnlySessionState);
  93. bool isNew = false;
  94. HttpSessionState session = null;
  95. if (handler != null)
  96. session = handler.UpdateContext (context, this, required, read_only, ref isNew);
  97. if (session != null) {
  98. if (isNew)
  99. session.SetNewSession (true);
  100. if (read_only)
  101. session = session.Clone ();
  102. context.SetSession (session);
  103. if (isNew && config.CookieLess) {
  104. string id = context.Session.SessionID;
  105. context.Request.SetHeader (HeaderName, id);
  106. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  107. context.Request.FilePath));
  108. } else if (isNew) {
  109. string id = context.Session.SessionID;
  110. HttpCookie cookie = new HttpCookie (CookieName, id);
  111. cookie.Path = UrlUtils.GetDirectory (context.Request.Path);
  112. context.Response.AppendCookie (cookie);
  113. }
  114. }
  115. // In the future, we might want to move the Async stuff down to
  116. // the interface level, if we're going to support other than
  117. // InProc, we might actually want to do things async, now we
  118. // simply fake it.
  119. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  120. result.Complete (true, o, null);
  121. if (isNew && Start != null)
  122. Start (this, args);
  123. return result;
  124. }
  125. void OnEndAcquireState (IAsyncResult result) { }
  126. internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  127. {
  128. OnEnd ();
  129. }
  130. internal void OnEnd ()
  131. {
  132. if (End != null)
  133. End (this, EventArgs.Empty);
  134. }
  135. public event EventHandler Start;
  136. public event EventHandler End;
  137. }
  138. }