2
0

SessionStateModule.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // System.Web.SessionState.SesionStateModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Stefan Görling ([email protected])
  7. //
  8. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  9. // (C) 2003 Stefan Görling (http://www.gorling.se)
  10. using System.Web;
  11. using System.Security.Cryptography;
  12. namespace System.Web.SessionState
  13. {
  14. [MonoTODO]
  15. public sealed class SessionStateModule : IHttpModule, IRequiresSessionState
  16. {
  17. static SessionConfig config;
  18. static Type handlerType;
  19. ISessionHandler handler;
  20. private RandomNumberGenerator rng;
  21. public SessionStateModule ()
  22. {
  23. rng = new RNGCryptoServiceProvider ();
  24. }
  25. internal RandomNumberGenerator Rng {
  26. get { return rng; }
  27. }
  28. public void Dispose ()
  29. {
  30. if (handler!=null)
  31. handler.Dispose();
  32. }
  33. [MonoTODO]
  34. public void Init (HttpApplication app)
  35. {
  36. if (config == null) {
  37. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  38. if (config == null)
  39. config = new SessionConfig (null);
  40. if (config.Mode == SessionStateMode.StateServer)
  41. throw new NotSupportedException ("StateServer mode is not supported.");
  42. if (config.Mode == SessionStateMode.SQLServer)
  43. handlerType = typeof (SessionSQLServerHandler);
  44. if (config.Mode == SessionStateMode.InProc)
  45. handlerType = typeof (SessionInProcHandler);
  46. }
  47. app.AddOnAcquireRequestStateAsync (
  48. new BeginEventHandler (OnBeginAcquireState),
  49. new EndEventHandler (OnEndAcquireState));
  50. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  51. app.EndRequest += new EventHandler (OnEndRequest);
  52. if (handlerType != null && handler == null) {
  53. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  54. handler.Init(app, config); //initialize
  55. }
  56. }
  57. void OnReleaseRequestState (object o, EventArgs args)
  58. {
  59. if (handler == null)
  60. return;
  61. HttpApplication application = (HttpApplication) o;
  62. HttpContext context = application.Context;
  63. handler.UpdateHandler (context, this);
  64. }
  65. void OnEndRequest (object o, EventArgs args)
  66. {
  67. }
  68. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  69. {
  70. HttpApplication application = (HttpApplication) o;
  71. HttpContext context = application.Context;
  72. bool isNew = false;
  73. if (handler != null)
  74. isNew = handler.UpdateContext (context, this);
  75. // In the future, we might want to move the Async stuff down to
  76. // the interface level, if we're going to support other than
  77. // InProc, we might actually want to do things async, now we
  78. // simply fake it.
  79. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  80. result.Complete (true, o, null);
  81. if (isNew && Start != null)
  82. Start (this, args);
  83. return result;
  84. }
  85. void OnEndAcquireState (IAsyncResult result)
  86. {
  87. }
  88. internal void OnEnd ()
  89. {
  90. if (End != null)
  91. End (this, EventArgs.Empty);
  92. }
  93. public event EventHandler Start;
  94. public event EventHandler End;
  95. }
  96. }