2
0

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