2
0

SessionIDManager.cs 4.1 KB

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