SessionStateModule.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. 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. [MonoTODO]
  38. public void Init (HttpApplication app)
  39. {
  40. if (config == null) {
  41. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  42. if (config == null)
  43. config = new SessionConfig (null);
  44. if (config.Mode == SessionStateMode.StateServer)
  45. handlerType = typeof (SessionStateServerHandler);
  46. if (config.Mode == SessionStateMode.SQLServer)
  47. handlerType = typeof (SessionSQLServerHandler);
  48. if (config.Mode == SessionStateMode.InProc)
  49. handlerType = typeof (SessionInProcHandler);
  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. public 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 isNew = false;
  91. if (handler != null)
  92. isNew = handler.UpdateContext (context, this);
  93. if (isNew && config.CookieLess) {
  94. string id = context.Session.SessionID;
  95. context.Request.SetHeader (HeaderName, id);
  96. context.Response.Redirect (UrlUtils.InsertSessionId (id,
  97. context.Request.FilePath));
  98. } else {
  99. string id = context.Session.SessionID;
  100. HttpCookie cookie = new HttpCookie (CookieName, id);
  101. cookie.Path = UrlUtils.GetDirectory (context.Request.Path);
  102. context.Response.AppendCookie (cookie);
  103. }
  104. // In the future, we might want to move the Async stuff down to
  105. // the interface level, if we're going to support other than
  106. // InProc, we might actually want to do things async, now we
  107. // simply fake it.
  108. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  109. result.Complete (true, o, null);
  110. if (isNew && Start != null)
  111. Start (this, args);
  112. return result;
  113. }
  114. void OnEndAcquireState (IAsyncResult result)
  115. {
  116. }
  117. internal void OnEnd ()
  118. {
  119. if (End != null)
  120. End (this, EventArgs.Empty);
  121. }
  122. public event EventHandler Start;
  123. public event EventHandler End;
  124. }
  125. }