2
0

SessionStateModule.cs 4.2 KB

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