SessionIDManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // System.Web.Compilation.SessionStateItemCollection
  3. //
  4. // Authors:
  5. // Marek Habersack ([email protected])
  6. //
  7. // (C) 2006 Marek Habersack
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Web;
  31. using System.Web.Configuration;
  32. using System.Web.Util;
  33. namespace System.Web.SessionState
  34. {
  35. public class SessionIDManager : ISessionIDManager
  36. {
  37. SessionStateSection config;
  38. public SessionIDManager ()
  39. {
  40. }
  41. public static int SessionIDMaxLength {
  42. get { return 80; }
  43. }
  44. // Todo: find use for the context parameter?
  45. public virtual string CreateSessionID (HttpContext context)
  46. {
  47. return SessionId.Create ();
  48. }
  49. public virtual string Decode (string id)
  50. {
  51. return HttpUtility.UrlDecode (id);
  52. }
  53. public virtual string Encode (string id)
  54. {
  55. return HttpUtility.UrlEncode (id);
  56. }
  57. public string GetSessionID (HttpContext context)
  58. {
  59. string ret = null;
  60. if (SessionStateModule.IsCookieLess (context, config)) {
  61. string tmp = context.Request.Headers [SessionStateModule.HeaderName];
  62. if (tmp != null)
  63. ret = Decode (tmp);
  64. } else {
  65. HttpCookie cookie = context.Request.Cookies [config.CookieName];
  66. if (cookie != null)
  67. ret = Decode (cookie.Value);
  68. }
  69. if (ret != null && ret.Length > SessionIDMaxLength)
  70. throw new HttpException ("The length of the session-identifier value retrieved from the HTTP request exceeds the SessionIDMaxLength value.");
  71. if (!Validate (ret))
  72. throw new HttpException ("Invalid session ID");
  73. return ret;
  74. }
  75. public void Initialize ()
  76. {
  77. config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
  78. }
  79. public bool InitializeRequest (HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
  80. {
  81. // TODO: Implement AutoDetect handling
  82. if (config.CookieLess) {
  83. supportSessionIDReissue = true;
  84. return false;
  85. } else {
  86. supportSessionIDReissue = false;
  87. return false;
  88. }
  89. }
  90. public void RemoveSessionID (HttpContext context)
  91. {
  92. context.Response.Cookies.Remove(config.CookieName);
  93. }
  94. // TODO: add code to check whether the response has already been sent
  95. public void SaveSessionID (HttpContext context, string id, out bool redirected, out bool cookieAdded)
  96. {
  97. if (!Validate (id))
  98. throw new HttpException ("Invalid session ID");
  99. HttpRequest request = context.Request;
  100. if (!SessionStateModule.IsCookieLess (context, config)) {
  101. HttpCookie cookie = new HttpCookie (config.CookieName, id);
  102. cookie.Path = request.ApplicationPath;
  103. context.Response.AppendCookie (cookie);
  104. cookieAdded = true;
  105. redirected = false;
  106. } else {
  107. request.SetHeader (SessionStateModule.HeaderName, id);
  108. cookieAdded = false;
  109. redirected = true;
  110. UriBuilder newUri = new UriBuilder (request.Url);
  111. newUri.Path = UrlUtils.InsertSessionId (id, request.FilePath);
  112. context.Response.Redirect (newUri.Uri.PathAndQuery, false);
  113. }
  114. }
  115. public virtual bool Validate (string id)
  116. {
  117. return true;
  118. }
  119. }
  120. }
  121. #endif