SessionIDManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. using System.Web;
  30. using System.Web.Configuration;
  31. using System.Web.Util;
  32. namespace System.Web.SessionState
  33. {
  34. public class SessionIDManager : ISessionIDManager
  35. {
  36. SessionStateSection config;
  37. public SessionIDManager ()
  38. {
  39. }
  40. public static int SessionIDMaxLength {
  41. get { return 80; }
  42. }
  43. // Todo: find use for the context parameter?
  44. public virtual string CreateSessionID (HttpContext context)
  45. {
  46. return SessionId.Create ();
  47. }
  48. public virtual string Decode (string id)
  49. {
  50. return HttpUtility.UrlDecode (id);
  51. }
  52. public virtual string Encode (string id)
  53. {
  54. return HttpUtility.UrlEncode (id);
  55. }
  56. public string GetSessionID (HttpContext context)
  57. {
  58. string ret = null;
  59. if (SessionStateModule.IsCookieLess (context, config)) {
  60. string tmp = context.Request.Headers [SessionStateModule.HeaderName];
  61. if (tmp != null)
  62. ret = Decode (tmp);
  63. } else {
  64. HttpCookie cookie = context.Request.Cookies [config.CookieName];
  65. if (cookie != null)
  66. ret = Decode (cookie.Value);
  67. }
  68. if (ret != null && ret.Length > SessionIDMaxLength)
  69. throw new HttpException ("The length of the session-identifier value retrieved from the HTTP request exceeds the SessionIDMaxLength value.");
  70. if (!Validate (ret))
  71. throw new HttpException ("Invalid session ID");
  72. return ret;
  73. }
  74. public void Initialize ()
  75. {
  76. config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
  77. }
  78. public bool InitializeRequest (HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
  79. {
  80. // TODO: Implement AutoDetect handling
  81. if (config.CookieLess) {
  82. supportSessionIDReissue = true;
  83. return false;
  84. } else {
  85. supportSessionIDReissue = false;
  86. return false;
  87. }
  88. }
  89. public void RemoveSessionID (HttpContext context)
  90. {
  91. context.Response.Cookies.Remove(config.CookieName);
  92. }
  93. // TODO: add code to check whether the response has already been sent
  94. public void SaveSessionID (HttpContext context, string id, out bool redirected, out bool cookieAdded)
  95. {
  96. if (!Validate (id))
  97. throw new HttpException ("Invalid session ID");
  98. HttpRequest request = context.Request;
  99. if (!SessionStateModule.IsCookieLess (context, config)) {
  100. HttpCookie cookie = new HttpCookie (config.CookieName, id);
  101. cookie.Path = request.ApplicationPath;
  102. context.Response.AppendCookie (cookie);
  103. cookieAdded = true;
  104. redirected = false;
  105. } else {
  106. request.SetHeader (SessionStateModule.HeaderName, id);
  107. cookieAdded = false;
  108. redirected = true;
  109. UriBuilder newUri = new UriBuilder (request.Url);
  110. newUri.Path = UrlUtils.InsertSessionId (id, request.FilePath);
  111. context.Response.Redirect (newUri.Uri.PathAndQuery, false);
  112. }
  113. }
  114. public virtual bool Validate (string id)
  115. {
  116. return true;
  117. }
  118. }
  119. }