2
0

SessionStateSection.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // System.Web.Configuration.SessionStateSection
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.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.ComponentModel;
  31. using System.Configuration;
  32. using System.Web.SessionState;
  33. #if NET_2_0
  34. namespace System.Web.Configuration {
  35. public sealed class SessionStateSection : ConfigurationSection
  36. {
  37. static ConfigurationProperty allowCustomSqlDatabaseProp;
  38. static ConfigurationProperty cookielessProp;
  39. static ConfigurationProperty cookieNameProp;
  40. static ConfigurationProperty customProviderProp;
  41. static ConfigurationProperty modeProp;
  42. static ConfigurationProperty partitionResolverTypeProp;
  43. static ConfigurationProperty providersProp;
  44. static ConfigurationProperty regenerateExpiredSessionIdProp;
  45. static ConfigurationProperty sessionIDManagerTypeProp;
  46. static ConfigurationProperty sqlCommandTimeoutProp;
  47. static ConfigurationProperty sqlConnectionStringProp;
  48. static ConfigurationProperty stateConnectionStringProp;
  49. static ConfigurationProperty stateNetworkTimeoutProp;
  50. static ConfigurationProperty timeoutProp;
  51. static ConfigurationProperty useHostingIdentityProp;
  52. static ConfigurationPropertyCollection properties;
  53. static ConfigurationElementProperty elementProperty;
  54. static SessionStateSection ()
  55. {
  56. allowCustomSqlDatabaseProp = new ConfigurationProperty ("allowCustomSqlDatabase", typeof (bool), false);
  57. cookielessProp = new ConfigurationProperty ("cookieless", typeof (string), null);
  58. cookieNameProp = new ConfigurationProperty ("cookieName", typeof (string), "ASP.NET_SessionId");
  59. customProviderProp = new ConfigurationProperty ("customProvider", typeof (string), "");
  60. modeProp = new ConfigurationProperty ("mode", typeof (SessionStateMode), SessionStateMode.InProc);
  61. partitionResolverTypeProp = new ConfigurationProperty ("partitionResolverType", typeof (string), "");
  62. providersProp = new ConfigurationProperty ("providers", typeof (ProviderSettingsCollection));
  63. regenerateExpiredSessionIdProp = new ConfigurationProperty ("regenerateExpiredSessionId", typeof (bool), true);
  64. sessionIDManagerTypeProp = new ConfigurationProperty ("sessionIDManagerType", typeof (string), "");
  65. sqlCommandTimeoutProp = new ConfigurationProperty ("sqlCommandTimeout", typeof (TimeSpan), TimeSpan.FromSeconds (30));
  66. sqlConnectionStringProp = new ConfigurationProperty ("sqlConnectionString", typeof (string), "data source=localhost;Integrated Security=SSPI");
  67. stateConnectionStringProp = new ConfigurationProperty ("stateConnectionString", typeof (string), "tcpip=loopback:42424");
  68. stateNetworkTimeoutProp = new ConfigurationProperty ("stateNetworkTimeout", typeof (TimeSpan), TimeSpan.FromSeconds (10),
  69. PropertyHelper.TimeSpanSecondsOrInfiniteConverter,
  70. PropertyHelper.PositiveTimeSpanValidator,
  71. ConfigurationPropertyOptions.None);
  72. timeoutProp = new ConfigurationProperty ("timeout", typeof (TimeSpan), TimeSpan.FromMinutes (20),
  73. PropertyHelper.TimeSpanMinutesOrInfiniteConverter,
  74. new TimeSpanValidator (new TimeSpan (0,1,0), TimeSpan.MaxValue),
  75. ConfigurationPropertyOptions.None);
  76. useHostingIdentityProp = new ConfigurationProperty ("useHostingIdentity", typeof (bool), true);
  77. properties = new ConfigurationPropertyCollection ();
  78. properties.Add (allowCustomSqlDatabaseProp);
  79. properties.Add (cookielessProp);
  80. properties.Add (cookieNameProp);
  81. properties.Add (customProviderProp);
  82. properties.Add (modeProp);
  83. properties.Add (partitionResolverTypeProp);
  84. properties.Add (providersProp);
  85. properties.Add (regenerateExpiredSessionIdProp);
  86. properties.Add (sessionIDManagerTypeProp);
  87. properties.Add (sqlCommandTimeoutProp);
  88. properties.Add (sqlConnectionStringProp);
  89. properties.Add (stateConnectionStringProp);
  90. properties.Add (stateNetworkTimeoutProp);
  91. properties.Add (timeoutProp);
  92. properties.Add (useHostingIdentityProp);
  93. elementProperty = new ConfigurationElementProperty (new CallbackValidator (typeof (SessionStateSection), ValidateElement));
  94. }
  95. [MonoTODO]
  96. protected override void PostDeserialize ()
  97. {
  98. base.PostDeserialize ();
  99. }
  100. [ConfigurationProperty ("allowCustomSqlDatabase", DefaultValue = "False")]
  101. public bool AllowCustomSqlDatabase {
  102. get { return (bool) base [allowCustomSqlDatabaseProp];}
  103. set { base[allowCustomSqlDatabaseProp] = value; }
  104. }
  105. [ConfigurationProperty ("cookieless")]
  106. public HttpCookieMode Cookieless {
  107. get { return ParseCookieMode ((string) base [cookielessProp]); }
  108. set { base[cookielessProp] = value.ToString(); }
  109. }
  110. [ConfigurationProperty ("cookieName", DefaultValue = "ASP.NET_SessionId")]
  111. public string CookieName {
  112. get { return (string) base [cookieNameProp];}
  113. set { base[cookieNameProp] = value; }
  114. }
  115. [ConfigurationProperty ("customProvider", DefaultValue = "")]
  116. public string CustomProvider {
  117. get { return (string) base [customProviderProp];}
  118. set { base[customProviderProp] = value; }
  119. }
  120. [ConfigurationProperty ("mode", DefaultValue = "InProc")]
  121. public SessionStateMode Mode {
  122. get { return (SessionStateMode) base [modeProp];}
  123. set { base[modeProp] = value; }
  124. }
  125. [ConfigurationProperty ("partitionResolverType", DefaultValue = "")]
  126. public string PartitionResolverType {
  127. get { return (string) base [partitionResolverTypeProp];}
  128. set { base[partitionResolverTypeProp] = value; }
  129. }
  130. [ConfigurationProperty ("providers")]
  131. public ProviderSettingsCollection Providers {
  132. get { return (ProviderSettingsCollection) base [providersProp];}
  133. }
  134. [ConfigurationProperty ("regenerateExpiredSessionId", DefaultValue = "True")]
  135. public bool RegenerateExpiredSessionId {
  136. get { return (bool) base [regenerateExpiredSessionIdProp];}
  137. set { base[regenerateExpiredSessionIdProp] = value; }
  138. }
  139. [ConfigurationProperty ("sessionIDManagerType", DefaultValue = "")]
  140. public string SessionIDManagerType {
  141. get { return (string) base [sessionIDManagerTypeProp];}
  142. set { base[sessionIDManagerTypeProp] = value; }
  143. }
  144. [TypeConverter (typeof (TimeSpanSecondsOrInfiniteConverter))]
  145. [ConfigurationProperty ("sqlCommandTimeout", DefaultValue = "00:00:30")]
  146. public TimeSpan SqlCommandTimeout {
  147. get { return (TimeSpan) base [sqlCommandTimeoutProp];}
  148. set { base[sqlCommandTimeoutProp] = value; }
  149. }
  150. [ConfigurationProperty ("sqlConnectionString", DefaultValue = "data source=localhost;Integrated Security=SSPI")]
  151. public string SqlConnectionString {
  152. get { return (string) base [sqlConnectionStringProp];}
  153. set { base[sqlConnectionStringProp] = value; }
  154. }
  155. [ConfigurationProperty ("stateConnectionString", DefaultValue = "tcpip=loopback:42424")]
  156. public string StateConnectionString {
  157. get { return (string) base [stateConnectionStringProp];}
  158. set { base[stateConnectionStringProp] = value; }
  159. }
  160. [TypeConverter (typeof (TimeSpanSecondsOrInfiniteConverter))]
  161. [ConfigurationProperty ("stateNetworkTimeout", DefaultValue = "00:00:10")]
  162. // LAMESPEC: MS lists no validator here but provides one in Properties.
  163. public TimeSpan StateNetworkTimeout {
  164. get { return (TimeSpan) base [stateNetworkTimeoutProp];}
  165. set { base[stateNetworkTimeoutProp] = value; }
  166. }
  167. [TypeConverter (typeof (TimeSpanMinutesOrInfiniteConverter))]
  168. [TimeSpanValidator (MinValueString = "00:01:00", MaxValueString = "10675199.02:48:05.4775807")]
  169. [ConfigurationProperty ("timeout", DefaultValue = "00:20:00")]
  170. public TimeSpan Timeout {
  171. get { return (TimeSpan) base [timeoutProp];}
  172. set { base[timeoutProp] = value; }
  173. }
  174. [ConfigurationProperty ("useHostingIdentity", DefaultValue = "True")]
  175. public bool UseHostingIdentity {
  176. get { return (bool) base [useHostingIdentityProp];}
  177. set { base[useHostingIdentityProp] = value; }
  178. }
  179. [MonoTODO]
  180. static void ValidateElement (object o)
  181. {
  182. /* XXX do some sort of element validation here? */
  183. }
  184. protected override ConfigurationElementProperty ElementProperty {
  185. get { return elementProperty; }
  186. }
  187. protected override ConfigurationPropertyCollection Properties {
  188. get { return properties; }
  189. }
  190. HttpCookieMode ParseCookieMode (string s)
  191. {
  192. if (s == "true") return HttpCookieMode.UseUri;
  193. else if (s == "false") return HttpCookieMode.UseCookies;
  194. else {
  195. try {
  196. return (HttpCookieMode)Enum.Parse (typeof(HttpCookieMode), s);
  197. }
  198. catch {
  199. return HttpCookieMode.UseCookies;
  200. }
  201. }
  202. }
  203. #region CompatabilityCode
  204. internal bool CookieLess {
  205. get { return Cookieless != HttpCookieMode.UseCookies; }
  206. set { Cookieless = value ? HttpCookieMode.UseUri : HttpCookieMode.UseCookies; }
  207. }
  208. #endregion
  209. }
  210. }
  211. #endif