HttpSessionState.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // System.Web.SessionState.HttpSessionState
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  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;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.Globalization;
  33. using System.Text;
  34. using System.Threading;
  35. using System.Web;
  36. namespace System.Web.SessionState {
  37. public sealed class HttpSessionState : ICollection, IEnumerable
  38. {
  39. private string _id;
  40. private SessionDictionary _dict;
  41. private HttpStaticObjectsCollection _staticObjects;
  42. private int _timeout;
  43. private bool _newSession;
  44. private bool _isCookieless;
  45. private SessionStateMode _mode;
  46. private bool _isReadonly;
  47. private bool _abandoned;
  48. internal HttpSessionState (string id,
  49. SessionDictionary dict,
  50. HttpStaticObjectsCollection staticObjects,
  51. int timeout,
  52. bool newSession,
  53. bool isCookieless,
  54. SessionStateMode mode,
  55. bool isReadonly)
  56. {
  57. _id = id;
  58. _dict = dict;
  59. _staticObjects = staticObjects.Clone ();
  60. _timeout = timeout;
  61. _newSession = newSession;
  62. _isCookieless = isCookieless;
  63. _mode = mode;
  64. _isReadonly = isReadonly;
  65. }
  66. internal HttpSessionState Clone ()
  67. {
  68. return new HttpSessionState (_id, _dict.Clone (), _staticObjects, _timeout, _newSession,
  69. _isCookieless, _mode, _isReadonly);
  70. }
  71. public int CodePage {
  72. get {
  73. HttpContext current = HttpContext.Current;
  74. if (current == null)
  75. return Encoding.Default.CodePage;
  76. return current.Response.ContentEncoding.CodePage;
  77. }
  78. set {
  79. HttpContext current = HttpContext.Current;
  80. if (current != null)
  81. current.Response.ContentEncoding = Encoding.GetEncoding (value);
  82. }
  83. }
  84. public HttpSessionState Contents {
  85. get { return this; }
  86. }
  87. public int Count {
  88. get { return _dict.Count; }
  89. }
  90. internal bool IsAbandoned {
  91. get { return _abandoned; }
  92. }
  93. public bool IsCookieless {
  94. get { return _isCookieless; }
  95. }
  96. public bool IsNewSession {
  97. get { return _newSession; }
  98. }
  99. public bool IsReadOnly {
  100. get { return _isReadonly; }
  101. }
  102. public bool IsSynchronized {
  103. get { return false; }
  104. }
  105. public object this [string key] {
  106. get { return _dict [key]; }
  107. set { _dict [key] = value; }
  108. }
  109. public object this [int index] {
  110. get { return _dict [index]; }
  111. set { _dict [index] = value; }
  112. }
  113. public NameObjectCollectionBase.KeysCollection Keys {
  114. get { return _dict.Keys; }
  115. }
  116. public int LCID {
  117. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  118. set { Thread.CurrentThread.CurrentCulture = new CultureInfo(value); }
  119. }
  120. public SessionStateMode Mode {
  121. get { return _mode; }
  122. }
  123. public string SessionID {
  124. get { return _id; }
  125. }
  126. public HttpStaticObjectsCollection StaticObjects {
  127. get { return _staticObjects; }
  128. }
  129. public object SyncRoot {
  130. get { return this; }
  131. }
  132. public int Timeout {
  133. get { return _timeout; }
  134. set {
  135. if (value < 1)
  136. throw new ArgumentException ("The argument to SetTimeout must be greater than 0.");
  137. _timeout = value;
  138. }
  139. }
  140. internal SessionDictionary SessionDictionary {
  141. get { return _dict; }
  142. }
  143. internal void SetNewSession (bool value)
  144. {
  145. _newSession = value;
  146. }
  147. public void Abandon ()
  148. {
  149. _abandoned = true;
  150. }
  151. public void Add (string name, object value)
  152. {
  153. _dict [name] = value;
  154. }
  155. public void Clear ()
  156. {
  157. if (_dict != null)
  158. _dict.Clear ();
  159. }
  160. public void CopyTo (Array array, int index)
  161. {
  162. NameObjectCollectionBase.KeysCollection all = Keys;
  163. for (int i = 0; i < all.Count; i++)
  164. array.SetValue (all.Get(i), i + index);
  165. }
  166. public IEnumerator GetEnumerator ()
  167. {
  168. return _dict.GetEnumerator ();
  169. }
  170. public void Remove (string name)
  171. {
  172. _dict.Remove (name);
  173. }
  174. public void RemoveAll ()
  175. {
  176. _dict.Clear ();
  177. }
  178. public void RemoveAt (int index)
  179. {
  180. _dict.RemoveAt (index);
  181. }
  182. }
  183. }