HttpSessionState.cs 5.1 KB

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