SessionStateSection.cs 9.8 KB

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