Merge pull request #1404 from woodsb02/mono-route
[mono.git] / mcs / class / System.Web / System.Web.Configuration_2.0 / SessionStateSection.cs
1 //
2 // System.Web.Configuration.SessionStateSection
3 //
4 // Authors:
5 //      Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.ComponentModel;
33 using System.Configuration;
34 using System.Web.SessionState;
35
36 namespace System.Web.Configuration
37 {
38         public sealed class SessionStateSection : ConfigurationSection
39         {
40                 internal static readonly string DefaultSqlConnectionString = "data source=localhost;Integrated Security=SSPI";
41                 
42                 static ConfigurationProperty allowCustomSqlDatabaseProp;
43                 static ConfigurationProperty cookielessProp;
44                 static ConfigurationProperty cookieNameProp;
45                 static ConfigurationProperty customProviderProp;
46                 static ConfigurationProperty modeProp;
47                 static ConfigurationProperty partitionResolverTypeProp;
48                 static ConfigurationProperty providersProp;
49                 static ConfigurationProperty regenerateExpiredSessionIdProp;
50                 static ConfigurationProperty sessionIDManagerTypeProp;
51                 static ConfigurationProperty sqlCommandTimeoutProp;
52                 static ConfigurationProperty sqlConnectionStringProp;
53                 static ConfigurationProperty stateConnectionStringProp;
54                 static ConfigurationProperty stateNetworkTimeoutProp;
55                 static ConfigurationProperty timeoutProp;
56                 static ConfigurationProperty useHostingIdentityProp;
57                 static ConfigurationProperty compressionEnabledProp;
58                 static ConfigurationProperty sqlConnectionRetryIntervalProp;
59                 static ConfigurationPropertyCollection properties;
60
61                 static ConfigurationElementProperty elementProperty;
62
63                 static SessionStateSection ()
64                 {
65                         allowCustomSqlDatabaseProp = new ConfigurationProperty ("allowCustomSqlDatabase", typeof (bool), false);
66                         cookielessProp = new ConfigurationProperty ("cookieless", typeof (string), null);
67                         cookieNameProp = new ConfigurationProperty ("cookieName", typeof (string), "ASP.NET_SessionId");
68                         customProviderProp = new ConfigurationProperty ("customProvider", typeof (string), "");
69                         modeProp = new ConfigurationProperty ("mode", typeof (SessionStateMode), SessionStateMode.InProc,
70                                                               new GenericEnumConverter (typeof (SessionStateMode)), null,
71                                                               ConfigurationPropertyOptions.None);
72                         partitionResolverTypeProp = new ConfigurationProperty ("partitionResolverType", typeof (string), "");
73                         providersProp = new ConfigurationProperty ("providers", typeof (ProviderSettingsCollection), null,
74                                                                    null, null, ConfigurationPropertyOptions.None);
75                         regenerateExpiredSessionIdProp = new ConfigurationProperty ("regenerateExpiredSessionId", typeof (bool), true);
76                         sessionIDManagerTypeProp = new ConfigurationProperty ("sessionIDManagerType", typeof (string), "");
77                         sqlCommandTimeoutProp = new ConfigurationProperty ("sqlCommandTimeout", typeof (TimeSpan), TimeSpan.FromSeconds (30),
78                                                                            PropertyHelper.TimeSpanSecondsOrInfiniteConverter, null,
79                                                                            ConfigurationPropertyOptions.None);
80                         sqlConnectionStringProp = new ConfigurationProperty ("sqlConnectionString", typeof (string), DefaultSqlConnectionString);
81                         stateConnectionStringProp = new ConfigurationProperty ("stateConnectionString", typeof (string), "tcpip=loopback:42424");
82                         stateNetworkTimeoutProp = new ConfigurationProperty ("stateNetworkTimeout", typeof (TimeSpan), TimeSpan.FromSeconds (10),
83                                                                              PropertyHelper.TimeSpanSecondsOrInfiniteConverter,
84                                                                              PropertyHelper.PositiveTimeSpanValidator,
85                                                                              ConfigurationPropertyOptions.None);
86                         timeoutProp = new ConfigurationProperty ("timeout", typeof (TimeSpan), TimeSpan.FromMinutes (20),
87                                                                  PropertyHelper.TimeSpanMinutesOrInfiniteConverter,
88                                                                  new TimeSpanValidator (new TimeSpan (0,1,0), TimeSpan.MaxValue),
89                                                                  ConfigurationPropertyOptions.None);
90                         useHostingIdentityProp = new ConfigurationProperty ("useHostingIdentity", typeof (bool), true);
91
92                         compressionEnabledProp = new ConfigurationProperty ("compressionEnabled", typeof (bool), false);
93                         sqlConnectionRetryIntervalProp = new ConfigurationProperty ("sqlConnectionRetryIntervalProp", typeof (TimeSpan), TimeSpan.FromSeconds (0),
94                                                                                     PropertyHelper.TimeSpanSecondsOrInfiniteConverter,
95                                                                                     PropertyHelper.PositiveTimeSpanValidator,
96                                                                                     ConfigurationPropertyOptions.None);
97                         properties = new ConfigurationPropertyCollection ();
98
99                         properties.Add (allowCustomSqlDatabaseProp);
100                         properties.Add (cookielessProp);
101                         properties.Add (cookieNameProp);
102                         properties.Add (customProviderProp);
103                         properties.Add (modeProp);
104                         properties.Add (partitionResolverTypeProp);
105                         properties.Add (providersProp);
106                         properties.Add (regenerateExpiredSessionIdProp);
107                         properties.Add (sessionIDManagerTypeProp);
108                         properties.Add (sqlCommandTimeoutProp);
109                         properties.Add (sqlConnectionStringProp);
110                         properties.Add (stateConnectionStringProp);
111                         properties.Add (stateNetworkTimeoutProp);
112                         properties.Add (timeoutProp);
113                         properties.Add (useHostingIdentityProp);
114                         properties.Add (compressionEnabledProp);
115                         properties.Add (sqlConnectionRetryIntervalProp);
116
117                         elementProperty = new ConfigurationElementProperty (new CallbackValidator (typeof (SessionStateSection), ValidateElement));
118                 }
119
120                 protected override void PostDeserialize ()
121                 {
122                         base.PostDeserialize ();
123                 }
124
125                 [ConfigurationProperty ("allowCustomSqlDatabase", DefaultValue = "False")]
126                 public bool AllowCustomSqlDatabase {
127                         get { return (bool) base [allowCustomSqlDatabaseProp];}
128                         set { base[allowCustomSqlDatabaseProp] = value; }
129                 }
130
131                 [ConfigurationProperty ("cookieless")]
132                 public HttpCookieMode Cookieless {
133                         get { return ParseCookieMode ((string) base [cookielessProp]); }
134                         set { base[cookielessProp] = value.ToString(); }
135                 }
136
137                 [ConfigurationProperty ("cookieName", DefaultValue = "ASP.NET_SessionId")]
138                 public string CookieName {
139                         get { return (string) base [cookieNameProp];}
140                         set { base[cookieNameProp] = value; }
141                 }
142
143                 [ConfigurationProperty ("customProvider", DefaultValue = "")]
144                 public string CustomProvider {
145                         get { return (string) base [customProviderProp];}
146                         set { base[customProviderProp] = value; }
147                 }
148
149                 [ConfigurationProperty ("mode", DefaultValue = "InProc")]
150                 public SessionStateMode Mode {
151                         get { return (SessionStateMode) base [modeProp];}
152                         set { base[modeProp] = value; }
153                 }
154
155                 [ConfigurationProperty ("partitionResolverType", DefaultValue = "")]
156                 public string PartitionResolverType {
157                         get { return (string) base [partitionResolverTypeProp];}
158                         set { base[partitionResolverTypeProp] = value; }
159                 }
160
161                 [ConfigurationProperty ("providers")]
162                 public ProviderSettingsCollection Providers {
163                         get { return (ProviderSettingsCollection) base [providersProp];}
164                 }
165
166                 [ConfigurationProperty ("regenerateExpiredSessionId", DefaultValue = "True")]
167                 public bool RegenerateExpiredSessionId {
168                         get { return (bool) base [regenerateExpiredSessionIdProp];}
169                         set { base[regenerateExpiredSessionIdProp] = value; }
170                 }
171
172                 [ConfigurationProperty ("sessionIDManagerType", DefaultValue = "")]
173                 public string SessionIDManagerType {
174                         get { return (string) base [sessionIDManagerTypeProp];}
175                         set { base[sessionIDManagerTypeProp] = value; }
176                 }
177
178                 [TypeConverter (typeof (TimeSpanSecondsOrInfiniteConverter))]
179                 [ConfigurationProperty ("sqlCommandTimeout", DefaultValue = "00:00:30")]
180                 public TimeSpan SqlCommandTimeout {
181                         get { return (TimeSpan) base [sqlCommandTimeoutProp];}
182                         set { base[sqlCommandTimeoutProp] = value; }
183                 }
184
185                 [ConfigurationProperty ("sqlConnectionString", DefaultValue = "data source=localhost;Integrated Security=SSPI")]
186                 public string SqlConnectionString {
187                         get { return (string) base [sqlConnectionStringProp];}
188                         set { base[sqlConnectionStringProp] = value; }
189                 }
190
191                 [ConfigurationProperty ("stateConnectionString", DefaultValue = "tcpip=loopback:42424")]
192                 public string StateConnectionString {
193                         get { return (string) base [stateConnectionStringProp];}
194                         set { base[stateConnectionStringProp] = value; }
195                 }
196
197                 [TypeConverter (typeof (TimeSpanSecondsOrInfiniteConverter))]
198                 [ConfigurationProperty ("stateNetworkTimeout", DefaultValue = "00:00:10")]
199                 // LAMESPEC: MS lists no validator here but provides one in Properties.
200                 public TimeSpan StateNetworkTimeout {
201                         get { return (TimeSpan) base [stateNetworkTimeoutProp];}
202                         set { base[stateNetworkTimeoutProp] = value; }
203                 }
204
205                 [TypeConverter (typeof (TimeSpanMinutesOrInfiniteConverter))]
206                 [TimeSpanValidator (MinValueString = "00:01:00", MaxValueString = "10675199.02:48:05.4775807")]
207                 [ConfigurationProperty ("timeout", DefaultValue = "00:20:00")]
208                 public TimeSpan Timeout {
209                         get { return (TimeSpan) base [timeoutProp];}
210                         set { base[timeoutProp] = value; }
211                 }
212
213                 [ConfigurationProperty ("useHostingIdentity", DefaultValue = "True")]
214                 public bool UseHostingIdentity {
215                         get { return (bool) base [useHostingIdentityProp];}
216                         set { base[useHostingIdentityProp] = value; }
217                 }
218
219                 [ConfigurationPropertyAttribute("compressionEnabled", DefaultValue = false)]
220                 public bool CompressionEnabled {
221                         get { return (bool) base [compressionEnabledProp]; }
222                         set { base [compressionEnabledProp] = value; }
223                 }
224
225                 [TypeConverterAttribute(typeof(TimeSpanSecondsOrInfiniteConverter))]
226                 [ConfigurationPropertyAttribute("sqlConnectionRetryInterval", DefaultValue = "00:00:00")]
227                 public TimeSpan SqlConnectionRetryInterval {
228                         get { return (TimeSpan) base [sqlConnectionRetryIntervalProp]; }
229                         set { base [sqlConnectionRetryIntervalProp] = value; }
230                 }
231                 
232                 static void ValidateElement (object o)
233                 {
234                         /* XXX do some sort of element validation here? */
235                 }
236
237                 protected internal override ConfigurationElementProperty ElementProperty {
238                         get { return elementProperty; }
239                 }
240
241                 protected internal override ConfigurationPropertyCollection Properties {
242                         get { return properties; }
243                 }
244
245                 HttpCookieMode ParseCookieMode (string s)
246                 {
247                         if (s == "true")
248                                 return HttpCookieMode.UseUri;
249                         else if (s == "false" || s == null)
250                                 return HttpCookieMode.UseCookies;
251                         else {
252                                 try {
253                                         return (HttpCookieMode)Enum.Parse (typeof(HttpCookieMode), s);
254                                 }
255                                 catch {
256                                         return HttpCookieMode.UseCookies;
257                                 }
258                         }
259                 }
260
261 #region CompatabilityCode
262                 internal bool CookieLess {
263                         get { return Cookieless != HttpCookieMode.UseCookies; }
264                         set { Cookieless = value ? HttpCookieMode.UseUri : HttpCookieMode.UseCookies; }
265                 }
266 #endregion
267
268         }
269 }