SessionStateModule.cs 4.6 KB

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