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 // Copyright (C) 2002,2003,2004,2005 Novell, Inc (http://www.novell.com)
10 // (C) 2003 Stefan Görling (http://www.gorling.se)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Caching;
33 using System.Web.Util;
34 using System.Security.Cryptography;
35 using System.Security.Permissions;
37 namespace System.Web.SessionState
39 // CAS - no InheritanceDemand here as the class is sealed
40 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 public sealed class SessionStateModule : IHttpModule
43 internal static readonly string CookieName = "ASPSESSION";
44 internal static readonly string HeaderName = "AspFilterSessionId";
45 static object locker = new object ();
48 static private SessionConfig config {
50 return (SessionConfig)AppDomain.CurrentDomain.GetData("SessionStateModule.config");
53 AppDomain.CurrentDomain.SetData("SessionStateModule.config", value);
56 static private Type handlerType {
58 return (Type)AppDomain.CurrentDomain.GetData("SessionStateModule.handlerType");
61 AppDomain.CurrentDomain.SetData("SessionStateModule.handlerType", value);
65 static SessionConfig config;
66 static Type handlerType;
68 ISessionHandler handler;
69 bool sessionForStaticFiles;
71 static RandomNumberGenerator rng = RandomNumberGenerator.Create ();
73 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
74 public SessionStateModule ()
78 internal RandomNumberGenerator Rng {
82 public void Dispose ()
88 SessionConfig GetConfig ()
94 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
96 config = new SessionConfig (null);
99 if (config.Mode == SessionStateMode.SQLServer || config.Mode == SessionStateMode.StateServer)
100 throw new NotImplementedException("You must use web.xml to specify session state handling");
102 if (config.Mode == SessionStateMode.StateServer)
103 handlerType = typeof (SessionStateServerHandler);
105 if (config.Mode == SessionStateMode.SQLServer)
106 handlerType = typeof (SessionSQLServerHandler);
108 if (config.Mode == SessionStateMode.InProc)
109 handlerType = typeof (SessionInProcHandler);
115 [EnvironmentPermission (SecurityAction.Assert, Read = "MONO_XSP_STATIC_SESSION")]
116 public void Init (HttpApplication app)
118 sessionForStaticFiles = (Environment.GetEnvironmentVariable ("MONO_XSP_STATIC_SESSION") != null);
119 SessionConfig cfg = GetConfig ();
120 if (handlerType == null)
124 app.BeginRequest += new EventHandler (OnBeginRequest);
126 app.AcquireRequestState += new EventHandler (OnAcquireState);
127 app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
128 app.EndRequest += new EventHandler (OnEndRequest);
130 if (handlerType != null && handler == null) {
131 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
132 handler.Init (this, app, cfg); //initialize
136 void OnBeginRequest (object o, EventArgs args)
138 HttpApplication application = (HttpApplication) o;
139 HttpContext context = application.Context;
140 string base_path = context.Request.BaseVirtualDir;
141 string id = UrlUtils.GetSessionId (base_path);
146 string new_path = UrlUtils.RemoveSessionId (base_path, context.Request.FilePath);
147 context.Request.SetFilePath (new_path);
148 context.Request.SetHeader (HeaderName, id);
149 context.Response.SetAppPathModifier (String.Concat ("(", id, ")"));
152 void OnReleaseRequestState (object o, EventArgs args)
157 HttpApplication application = (HttpApplication) o;
158 HttpContext context = application.Context;
159 handler.UpdateHandler (context, this);
162 void OnEndRequest (object o, EventArgs args)
166 void OnAcquireState (object o, EventArgs args)
168 HttpApplication application = (HttpApplication) o;
169 HttpContext context = application.Context;
171 bool required = (context.Handler is IRequiresSessionState);
173 // This is a hack. Sites that use Session in global.asax event handling code
174 // are not supposed to get a Session object for static files, but seems that
175 // IIS handles those files before getting there and thus they are served without
177 // As a workaround, setting MONO_XSP_STATIC_SESSION variable make this work
178 // on mono, but you lose performance when serving static files.
179 if (sessionForStaticFiles && context.Handler is StaticFileHandler)
183 bool read_only = (context.Handler is IReadOnlySessionState);
186 HttpSessionState session = null;
188 session = handler.UpdateContext (context, this, required, read_only, ref isNew);
190 if (session != null) {
192 session.SetNewSession (true);
195 session = session.Clone ();
197 context.SetSession (session);
199 HttpRequest request = context.Request;
200 HttpResponse response = context.Response;
201 string id = context.Session.SessionID;
202 if (isNew && config.CookieLess) {
203 request.SetHeader (HeaderName, id);
204 response.Redirect (UrlUtils.InsertSessionId (id, request.FilePath));
206 HttpCookie cookie = new HttpCookie (CookieName, id);
207 cookie.Path = request.ApplicationPath;
208 context.Response.AppendCookie (cookie);
216 void OnSessionStart ()
219 Start (this, EventArgs.Empty);
222 internal void OnSessionRemoved (string key, object value, CacheItemRemovedReason reason)
224 // Only invoked for InProc (see msdn2 docs on SessionStateModule.End)
225 if (GetConfig ().Mode == SessionStateMode.InProc)
226 HttpApplicationFactory.InvokeSessionEnd (value);
229 public event EventHandler Start;
231 // This event is public, but only Session_[On]End in global.asax will be invoked if present.
232 public event EventHandler End;