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