2 // System.Web.Compilation.SessionStateItemCollection
5 // Marek Habersack (grendello@gmail.com)
7 // (C) 2006 Marek Habersack
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Configuration;
33 using System.Web.Util;
35 namespace System.Web.SessionState
37 public class SessionIDManager : ISessionIDManager
39 SessionStateSection config;
41 public SessionIDManager ()
45 public static int SessionIDMaxLength {
49 // Todo: find use for the context parameter?
50 public virtual string CreateSessionID (HttpContext context)
52 return SessionId.Create ();
55 public virtual string Decode (string id)
57 return HttpUtility.UrlDecode (id);
60 public virtual string Encode (string id)
62 return HttpUtility.UrlEncode (id);
65 public string GetSessionID (HttpContext context)
69 if (SessionStateModule.IsCookieLess (context, config)) {
70 string tmp = context.Request.Headers [SessionStateModule.HeaderName];
74 HttpCookie cookie = context.Request.Cookies [config.CookieName];
76 ret = Decode (cookie.Value);
79 if (ret != null && ret.Length > SessionIDMaxLength)
80 throw new HttpException ("The length of the session-identifier value retrieved from the HTTP request exceeds the SessionIDMaxLength value.");
82 throw new HttpException ("Invalid session ID");
87 public void Initialize ()
89 config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
92 public bool InitializeRequest (HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
94 // TODO: Implement AutoDetect handling
95 if (config.CookieLess) {
96 supportSessionIDReissue = true;
99 supportSessionIDReissue = false;
104 public void RemoveSessionID (HttpContext context)
106 context.Response.Cookies.Remove(config.CookieName);
109 // TODO: add code to check whether the response has already been sent
110 public void SaveSessionID (HttpContext context, string id, out bool redirected, out bool cookieAdded)
113 throw new HttpException ("Invalid session ID");
115 HttpRequest request = context.Request;
116 if (!SessionStateModule.IsCookieLess (context, config)) {
117 HttpCookie cookie = new HttpCookie (config.CookieName, id);
118 cookie.Path = request.ApplicationPath;
119 context.Response.AppendCookie (cookie);
123 request.SetHeader (SessionStateModule.HeaderName, id);
126 UriBuilder newUri = new UriBuilder (request.Url);
127 newUri.Path = UrlUtils.InsertSessionId (id, request.FilePath);
128 context.Response.Redirect (newUri.Uri.PathAndQuery, false);
132 public virtual bool Validate (string id)