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,2004,2005 Novell, Inc (http://www.novell.com)
10 // (C) 2003 Stefan Görling (http://www.gorling.se)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Web.Caching;
35 using System.Web.Util;
36 using System.Security.Cryptography;
38 namespace System.Web.SessionState
40 public sealed class SessionStateModule : IHttpModule
42 internal static readonly string CookieName = "ASPSESSION";
43 internal static readonly string HeaderName = "AspFilterSessionId";
46 static SessionConfig config;
47 static Type handlerType;
49 static private SessionConfig config {
51 return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
54 AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
57 static private Type handlerType {
59 return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
62 AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
66 ISessionHandler handler;
67 bool sessionForStaticFiles;
69 static RandomNumberGenerator rng = new RNGCryptoServiceProvider ();
71 public SessionStateModule ()
75 internal RandomNumberGenerator Rng {
79 public void Dispose ()
85 public void Init (HttpApplication app)
87 sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
89 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
91 config = new SessionConfig (null);
93 if (config.Mode == SessionStateMode.StateServer)
94 handlerType = typeof (SessionStateServerHandler);
96 if (config.Mode == SessionStateMode.SQLServer)
97 handlerType = typeof (SessionSQLServerHandler);
99 if (config.Mode == SessionStateMode.InProc)
100 handlerType = typeof (SessionInProcHandler);
102 if (config.Mode == SessionStateMode.Off)
106 if (config.CookieLess)
107 app.BeginRequest += new EventHandler (OnBeginRequest);
109 app.AddOnAcquireRequestStateAsync (
110 new BeginEventHandler (OnBeginAcquireState),
111 new EndEventHandler (OnEndAcquireState));
113 app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
114 app.EndRequest += new EventHandler (OnEndRequest);
116 if (handlerType != null && handler == null) {
117 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
118 handler.Init (this, app, config); //initialize
122 void OnBeginRequest (object o, EventArgs args)
124 HttpApplication application = (HttpApplication) o;
125 HttpContext context = application.Context;
126 string base_path = context.Request.BaseVirtualDir;
127 string id = UrlUtils.GetSessionId (base_path);
132 context.Request.SetCurrentExePath (UrlUtils.RemoveSessionId (base_path,
133 context.Request.FilePath));
134 context.Request.SetHeader (HeaderName, id);
135 context.Response.SetAppPathModifier (String.Format ("({0})", id));
138 void OnReleaseRequestState (object o, EventArgs args)
143 HttpApplication application = (HttpApplication) o;
144 HttpContext context = application.Context;
145 handler.UpdateHandler (context, this);
148 void OnEndRequest (object o, EventArgs args)
152 IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
154 HttpApplication application = (HttpApplication) o;
155 HttpContext context = application.Context;
157 bool required = (context.Handler is IRequiresSessionState);
159 // This is a hack. Sites that use Session in global.asax event handling code
160 // are not supposed to get a Session object for static files, but seems that
161 // IIS handles those files before getting there and thus they are served without
163 // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
164 // on mono, but you lose performance when serving static files.
165 if (sessionForStaticFiles && context.Handler is StaticFileHandler)
169 bool read_only = (context.Handler is IReadOnlySessionState);
172 HttpSessionState session = null;
174 session = handler.UpdateContext (context, this, required, read_only, ref isNew);
176 if (session != null) {
178 session.SetNewSession (true);
181 session = session.Clone ();
183 context.SetSession (session);
185 if (isNew && config.CookieLess) {
186 string id = context.Session.SessionID;
187 context.Request.SetHeader (HeaderName, id);
188 context.Response.Redirect (UrlUtils.InsertSessionId (id,
189 context.Request.FilePath));
191 string id = context.Session.SessionID;
192 HttpCookie cookie = new HttpCookie (CookieName, id);
193 cookie.Path = UrlUtils.GetDirectory (context.Request.ApplicationPath);
194 context.Response.AppendCookie (cookie);
198 // In the future, we might want to move the Async stuff down to
199 // the interface level, if we're going to support other than
200 // InProc, we might actually want to do things async, now we
202 HttpAsyncResult result=new HttpAsyncResult (cb,this);
203 result.Complete (true, o, null);
204 if (isNew && Start != null)
210 void OnEndAcquireState (IAsyncResult result) { }
212 internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
217 internal void OnEnd ()
221 End (this, EventArgs.Empty);
225 public event EventHandler Start;
226 public event EventHandler End;