SessionStateModule.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. //
  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.Web;
  32. using System.Web.Caching;
  33. using System.Web.Util;
  34. using System.Security.Cryptography;
  35. namespace System.Web.SessionState
  36. {
  37. public sealed class SessionStateModule : IHttpModule
  38. {
  39. internal static readonly string CookieName = "ASPSESSION";
  40. internal static readonly string HeaderName = "AspFilterSessionId";
  41. static SessionConfig config;
  42. static Type handlerType;
  43. ISessionHandler handler;
  44. private RandomNumberGenerator rng;
  45. public SessionStateModule ()
  46. {
  47. rng = new RNGCryptoServiceProvider ();
  48. }
  49. internal RandomNumberGenerator Rng {
  50. get { return rng; }
  51. }
  52. public void Dispose ()
  53. {
  54. if (handler!=null)
  55. handler.Dispose();
  56. }
  57. public void Init (HttpApplication app)
  58. {
  59. if (config == null) {
  60. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  61. if (config == null)
  62. config = new SessionConfig (null);
  63. if (config.Mode == SessionStateMode.StateServer)
  64. handlerType = typeof (SessionStateServerHandler);
  65. if (config.Mode == SessionStateMode.SQLServer)
  66. handlerType = typeof (SessionSQLServerHandler);
  67. if (config.Mode == SessionStateMode.InProc)
  68. handlerType = typeof (SessionInProcHandler);
  69. if (config.Mode == SessionStateMode.Off)
  70. return;
  71. }
  72. if (config.CookieLess)
  73. app.BeginRequest += new EventHandler (OnBeginRequest);
  74. app.AddOnAcquireRequestStateAsync (
  75. new BeginEventHandler (OnBeginAcquireState),
  76. new EndEventHandler (OnEndAcquireState));
  77. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  78. app.EndRequest += new EventHandler (OnEndRequest);
  79. if (handlerType != null && handler == null) {
  80. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  81. handler.Init (this, app, config); //initialize
  82. }
  83. }
  84. void OnBeginRequest (object o, EventArgs args)
  85. {
  86. HttpApplication application = (HttpApplication) o;
  87. HttpContext context = application.Context;
  88. string base_path = context.Request.BaseVirtualDir;
  89. string id = UrlUtils.GetSessionId (base_path);
  90. if (id == null)
  91. return;
  92. context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
  93. context.Request.FilePath));
  94. context.Request.SetHeader (HeaderName, id);
  95. context.Response.SetAppPathModifier (String.Format ("({0})", id));
  96. }
  97. void OnReleaseRequestState (object o, EventArgs args)
  98. {
  99. if (handler == null)
  100. return;
  101. HttpApplication application = (HttpApplication) o;
  102. HttpContext context = application.Context;
  103. handler.UpdateHandler (context, this);
  104. }
  105. void OnEndRequest (object o, EventArgs args)
  106. {
  107. }
  108. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  109. {
  110. HttpApplication application = (HttpApplication) o;
  111. HttpContext context = application.Context;
  112. bool required = (context.Handler is IRequiresSessionState);
  113. bool read_only = (context.Handler is IReadOnlySessionState);
  114. bool isNew = false;
  115. HttpSessionState session = null;
  116. if (handler != null)
  117. session = handler.UpdateContext (context, this, required, read_only, ref isNew);
  118. if (session != null) {
  119. if (isNew)
  120. session.SetNewSession (true);
  121. if (read_only)
  122. session = session.Clone ();
  123. context.SetSession (session);
  124. if (isNew && config.CookieLess) {
  125. string id = context.Session.SessionID;
  126. context.Request.SetHeader (HeaderName, id);
  127. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  128. context.Request.FilePath));
  129. } else if (isNew) {
  130. string id = context.Session.SessionID;
  131. HttpCookie cookie = new HttpCookie (CookieName, id);
  132. cookie.Path = UrlUtils.GetDirectory (context.Request.ApplicationPath);
  133. context.Response.AppendCookie (cookie);
  134. }
  135. }
  136. // In the future, we might want to move the Async stuff down to
  137. // the interface level, if we're going to support other than
  138. // InProc, we might actually want to do things async, now we
  139. // simply fake it.
  140. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  141. result.Complete (true, o, null);
  142. if (isNew && Start != null)
  143. Start (this, args);
  144. return result;
  145. }
  146. void OnEndAcquireState (IAsyncResult result) { }
  147. internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
  148. {
  149. OnEnd ();
  150. }
  151. internal void OnEnd ()
  152. {
  153. if (End != null)
  154. End (this, EventArgs.Empty);
  155. }
  156. public event EventHandler Start;
  157. public event EventHandler End;
  158. }
  159. }