2 // System.Web.SessionState.SesionStateModule
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Stefan Görling (stefan@gorling.se)
8 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2003 Stefan Görling (http://www.gorling.se)
12 using System.Security.Cryptography;
14 namespace System.Web.SessionState
17 public sealed class SessionStateModule : IHttpModule, IRequiresSessionState
19 static SessionConfig config;
20 static Type handlerType;
21 ISessionHandler handler;
23 private RandomNumberGenerator rng;
25 public SessionStateModule ()
27 rng = new RNGCryptoServiceProvider ();
30 internal RandomNumberGenerator Rng {
34 public void Dispose ()
41 public void Init (HttpApplication app)
44 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
46 config = new SessionConfig (null);
48 if (config.Mode == SessionStateMode.StateServer)
49 handlerType = typeof (SessionStateServerHandler);
51 if (config.Mode == SessionStateMode.SQLServer)
52 handlerType = typeof (SessionSQLServerHandler);
54 if (config.Mode == SessionStateMode.InProc)
55 handlerType = typeof (SessionInProcHandler);
58 app.AddOnAcquireRequestStateAsync (
59 new BeginEventHandler (OnBeginAcquireState),
60 new EndEventHandler (OnEndAcquireState));
62 app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
63 app.EndRequest += new EventHandler (OnEndRequest);
65 if (handlerType != null && handler == null) {
66 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
67 handler.Init(app, config); //initialize
71 void OnReleaseRequestState (object o, EventArgs args)
76 HttpApplication application = (HttpApplication) o;
77 HttpContext context = application.Context;
78 handler.UpdateHandler (context, this);
81 void OnEndRequest (object o, EventArgs args)
85 IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
88 HttpApplication application = (HttpApplication) o;
89 HttpContext context = application.Context;
93 isNew = handler.UpdateContext (context, this);
95 // In the future, we might want to move the Async stuff down to
96 // the interface level, if we're going to support other than
97 // InProc, we might actually want to do things async, now we
99 HttpAsyncResult result=new HttpAsyncResult (cb,this);
100 result.Complete (true, o, null);
101 if (isNew && Start != null)
107 void OnEndAcquireState (IAsyncResult result)
111 internal void OnEnd ()
114 End (this, EventArgs.Empty);
117 public event EventHandler Start;
118 public event EventHandler End;