2 // System.Web.SessionState.SesionStateModule
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Stefan Görling (stefan@gorling.se)
7 // Jackson Harper (jackson@ximian.com)
9 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
10 // (C) 2003 Stefan Görling (http://www.gorling.se)
13 using System.Web.Util;
14 using System.Security.Cryptography;
16 namespace System.Web.SessionState
19 public sealed class SessionStateModule : IHttpModule, IRequiresSessionState
21 internal static readonly string CookieName = "ASPSESSION";
22 internal static readonly string HeaderName = "AspFilterSessionId";
24 static SessionConfig config;
25 static Type handlerType;
26 ISessionHandler handler;
28 private RandomNumberGenerator rng;
30 public SessionStateModule ()
32 rng = new RNGCryptoServiceProvider ();
35 internal RandomNumberGenerator Rng {
39 public void Dispose ()
46 public void Init (HttpApplication app)
49 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
51 config = new SessionConfig (null);
53 if (config.Mode == SessionStateMode.StateServer)
54 handlerType = typeof (SessionStateServerHandler);
56 if (config.Mode == SessionStateMode.SQLServer)
57 handlerType = typeof (SessionSQLServerHandler);
59 if (config.Mode == SessionStateMode.InProc)
60 handlerType = typeof (SessionInProcHandler);
63 if (config.CookieLess)
64 app.BeginRequest += new EventHandler (OnBeginRequest);
66 app.AddOnAcquireRequestStateAsync (
67 new BeginEventHandler (OnBeginAcquireState),
68 new EndEventHandler (OnEndAcquireState));
70 app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
71 app.EndRequest += new EventHandler (OnEndRequest);
73 if (handlerType != null && handler == null) {
74 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
75 handler.Init(app, config); //initialize
79 public void OnBeginRequest (object o, EventArgs args)
81 HttpApplication application = (HttpApplication) o;
82 HttpContext context = application.Context;
83 string base_path = context.Request.BaseVirtualDir;
84 string id = UrlUtils.GetSessionId (base_path);
89 context.Request.SetFilePath (UrlUtils.RemoveSessionId (base_path,
90 context.Request.FilePath));
91 context.Request.SetHeader (HeaderName, id);
94 void OnReleaseRequestState (object o, EventArgs args)
99 HttpApplication application = (HttpApplication) o;
100 HttpContext context = application.Context;
101 handler.UpdateHandler (context, this);
104 void OnEndRequest (object o, EventArgs args)
108 IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
110 HttpApplication application = (HttpApplication) o;
111 HttpContext context = application.Context;
115 isNew = handler.UpdateContext (context, this);
117 if (isNew && config.CookieLess) {
118 string id = context.Session.SessionID;
119 context.Request.SetHeader (HeaderName, id);
120 context.Response.Redirect (UrlUtils.InsertSessionId (id,
121 context.Request.FilePath));
123 string id = context.Session.SessionID;
124 HttpCookie cookie = new HttpCookie (CookieName, id);
125 cookie.Path = UrlUtils.GetDirectory (context.Request.Path);
126 context.Response.AppendCookie (cookie);
129 // In the future, we might want to move the Async stuff down to
130 // the interface level, if we're going to support other than
131 // InProc, we might actually want to do things async, now we
133 HttpAsyncResult result=new HttpAsyncResult (cb,this);
134 result.Complete (true, o, null);
135 if (isNew && Start != null)
141 void OnEndAcquireState (IAsyncResult result)
145 internal void OnEnd ()
148 End (this, EventArgs.Empty);
151 public event EventHandler Start;
152 public event EventHandler End;