Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web / Configuration / SessionStateSection.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SessionStateSection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 namespace System.Web.Configuration {
8     using System;
9     using System.Xml;
10     using System.Configuration;
11     using System.Collections.Specialized;
12     using System.Collections;
13     using System.Globalization;
14     using System.IO;
15     using System.Text;
16     using System.ComponentModel;
17     using System.Web.SessionState;
18     using System.Diagnostics;
19     using System.Security.Permissions;
20
21     /*         <!-- sessionState Attributes:
22                 mode="[Off|InProc|StateServer|SQLServer|Custom]"
23                 stateConnectionString="tcpip=server:port"
24                 stateNetworkTimeout="timeout for network operations with State Server, in seconds"
25                 sqlConnectionString="valid System.Data.SqlClient.SqlConnection string, minus Initial Catalog"
26                 sqlCommandTimeout="timeout for SQL commands sent to SQL Server, in seconds"
27                 sqlConnectionRetryInterval="the interval the SQL State provider will retry opening connections and executing SQL commands when fatal errors occur, in seconds"
28                 customProvider="name of the custom provider"
29                 cookieless="[true|false|UseCookies|UseUri|AutoDetect|UseDeviceProfile]"
30                 cookieName="To override the default cookie name used for storing session ID"
31                 allowCustomSqlDatabase="[true|false]" - If true, the user can specify the Initial Catalog value in sqlConnectionString
32                 compressionEnabled="[true|false]"
33                 timeout="timeout in minutes"
34                 partitionResolverType="[fully qualified type of partition resolver]"
35                 useHostingIdentity="[true|false]"
36                 sessionIDManagerType="[fully qualified type of session ID Manager]"
37
38               Child nodes:
39                 <providers>              Custom store providers (class must inherit SessionStateStoreProviderBase)
40                     <add                 Add a provider
41                         name="string"    Name to identify this provider instance by
42                         type="string"    Class that implements ISessionStateStore
43                         provider-specific-configuration />
44
45                     <remove              Remove a provider
46                         name="string" /> Name of provider to remove
47                     <clear/>             Remove all providers
48                 </providers>
49         -->
50         <sessionState
51             mode="InProc"
52             stateConnectionString="tcpip=loopback:42424"
53             stateNetworkTimeout="10"
54             sqlConnectionString="data source=localhost;Integrated Security=SSPI"
55             sqlCommandTimeout="30"
56             customProvider=""
57             cookieless="false"
58             allowCustomSqlDatabase="false"
59             compressionEnabled="false"
60             regenerateExpiredSessionId="false"
61             timeout="20"
62         >
63             <providers>
64             </providers>
65         </sessionState>
66
67  */
68     public sealed class SessionStateSection : ConfigurationSection {
69         private static readonly ConfigurationElementProperty s_elemProperty =
70             new ConfigurationElementProperty(new CallbackValidator(typeof(SessionStateSection), Validate));
71
72
73         private static ConfigurationPropertyCollection _properties;
74
75         private static readonly ConfigurationProperty _propMode =
76             new ConfigurationProperty("mode",
77                                         typeof(SessionStateMode),
78                                         SessionStateModule.MODE_DEFAULT,
79                                         ConfigurationPropertyOptions.None);
80
81         private static readonly ConfigurationProperty _propStateConnectionString =
82             new ConfigurationProperty("stateConnectionString",
83                                         typeof(string),
84                                         SessionStateModule.STATE_CONNECTION_STRING_DEFAULT,
85                                         ConfigurationPropertyOptions.None);
86
87         private static readonly ConfigurationProperty _propStateNetworkTimeout =
88             new ConfigurationProperty("stateNetworkTimeout",
89                                         typeof(TimeSpan),
90 #if FEATURE_PAL // FEATURE_PAL does not enable OutOfProcSessionStore
91                                         TimeSpan.FromSeconds(600),
92 #else // FEATURE_PAL
93                                         TimeSpan.FromSeconds((long)
94                                             OutOfProcSessionStateStore.STATE_NETWORK_TIMEOUT_DEFAULT),
95 #endif // FEATURE_PAL
96                                         StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
97                                         StdValidatorsAndConverters.PositiveTimeSpanValidator,
98                                         ConfigurationPropertyOptions.None);
99
100         private static readonly ConfigurationProperty _propSqlConnectionString =
101             new ConfigurationProperty("sqlConnectionString",
102                                         typeof(string),
103 #if FEATURE_PAL // FEATURE_PAL does not enable SessionStateModule
104                                         "data source=localhost;Integrated Security=SSPI",
105 #else // FEATURE_PAL
106                                         SessionStateModule.SQL_CONNECTION_STRING_DEFAULT,
107 #endif // FEATURE_PAL
108                                         ConfigurationPropertyOptions.None);
109
110         private static readonly ConfigurationProperty _propSqlCommandTimeout =
111             new ConfigurationProperty("sqlCommandTimeout",
112                                         typeof(TimeSpan),
113 #if FEATURE_PAL // FEATURE_PAL does not enable SqlSessionStateStore
114                                         TimeSpan.FromSeconds(1800),
115 #else // FEATURE_PAL
116                                         TimeSpan.FromSeconds((long)
117                                             SqlSessionStateStore.SQL_COMMAND_TIMEOUT_DEFAULT),
118 #endif // FEATURE_PAL
119                                         StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
120                                         null,
121                                         ConfigurationPropertyOptions.None);
122
123         private static readonly ConfigurationProperty _propSqlConnectionRetryInterval =
124                     new ConfigurationProperty("sqlConnectionRetryInterval",
125                                                 typeof(TimeSpan),
126                                                 TimeSpan.FromSeconds(0),
127                                                 StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
128                                                 null,
129                                                 ConfigurationPropertyOptions.None);
130
131         private static readonly ConfigurationProperty _propCustomProvider =
132             new ConfigurationProperty("customProvider",
133                                         typeof(string),
134                                         String.Empty,
135                                         ConfigurationPropertyOptions.None);
136
137         private static readonly ConfigurationProperty _propCookieless =
138             new ConfigurationProperty("cookieless",
139                                         typeof(string),
140                                         SessionIDManager.COOKIEMODE_DEFAULT.ToString(),
141                                         ConfigurationPropertyOptions.None);
142
143         private static readonly ConfigurationProperty _propCookieName =
144             new ConfigurationProperty("cookieName",
145                                         typeof(string),
146                                         SessionIDManager.SESSION_COOKIE_DEFAULT,
147                                         ConfigurationPropertyOptions.None);
148
149         private static readonly ConfigurationProperty _propTimeout =
150             new ConfigurationProperty("timeout",
151                                         typeof(TimeSpan),
152                                         TimeSpan.FromMinutes((long)SessionStateModule.TIMEOUT_DEFAULT),
153                                         StdValidatorsAndConverters.TimeSpanMinutesOrInfiniteConverter,
154                                         new TimeSpanValidator(TimeSpan.FromMinutes(1), TimeSpan.MaxValue),
155                                         ConfigurationPropertyOptions.None);
156         private static readonly ConfigurationProperty _propAllowCustomSqlDatabase =
157             new ConfigurationProperty("allowCustomSqlDatabase",
158                                         typeof(bool),
159                                         false,
160                                         ConfigurationPropertyOptions.None);
161
162         private static readonly ConfigurationProperty _propCompressionEnabled =
163             new ConfigurationProperty("compressionEnabled",
164                                         typeof(bool),
165                                         false,
166                                         ConfigurationPropertyOptions.None);
167
168
169         //        private static readonly ConfigurationProperty _propLockAttributes =
170         //            new ConfigurationProperty("lockAttributes",
171         //                                    typeof(string),
172         //                                    "",
173         //                                    ConfigurationPropertyOptions.None);
174
175         private static readonly ConfigurationProperty _propProviders =
176             new ConfigurationProperty("providers",
177                                         typeof(ProviderSettingsCollection),
178                                         null,
179                                         ConfigurationPropertyOptions.None);
180
181         private static readonly ConfigurationProperty _propRegenerateExpiredSessionId =
182             new ConfigurationProperty("regenerateExpiredSessionId",
183                                         typeof(bool),
184                                         true,
185                                         ConfigurationPropertyOptions.None);
186
187         private static readonly ConfigurationProperty _propPartitionResolverType =
188             new ConfigurationProperty("partitionResolverType",
189                                         typeof(string),
190                                         String.Empty,
191                                         ConfigurationPropertyOptions.None);
192
193         private static readonly ConfigurationProperty _propUseHostingIdentity =
194             new ConfigurationProperty("useHostingIdentity",
195                                         typeof(bool),
196                                         true,
197                                         ConfigurationPropertyOptions.None);
198
199         private static readonly ConfigurationProperty _propSessionIDManagerType =
200             new ConfigurationProperty("sessionIDManagerType",
201                                         typeof(string),
202                                         String.Empty,
203                                         ConfigurationPropertyOptions.None);
204
205         private HttpCookieMode cookielessCache = SessionIDManager.COOKIEMODE_DEFAULT;
206         private bool cookielessCached = false;
207         private bool regenerateExpiredSessionIdCache = false;
208         private bool regenerateExpiredSessionIdCached = false;
209         static SessionStateSection() {
210             // Property initialization
211             _properties = new ConfigurationPropertyCollection();
212             _properties.Add(_propMode);
213             _properties.Add(_propStateConnectionString);
214             _properties.Add(_propStateNetworkTimeout);
215             _properties.Add(_propSqlConnectionString);
216             _properties.Add(_propSqlCommandTimeout);
217             _properties.Add(_propSqlConnectionRetryInterval);
218             _properties.Add(_propCustomProvider);
219             _properties.Add(_propCookieless);
220             _properties.Add(_propCookieName);
221             _properties.Add(_propTimeout);
222             _properties.Add(_propAllowCustomSqlDatabase);
223             _properties.Add(_propCompressionEnabled);
224             //            _properties.Add(_propLockAttributes);
225             _properties.Add(_propProviders);
226             _properties.Add(_propRegenerateExpiredSessionId);
227             _properties.Add(_propPartitionResolverType);
228             _properties.Add(_propUseHostingIdentity);
229             _properties.Add(_propSessionIDManagerType);
230         }
231
232         public SessionStateSection() {
233         }
234
235         protected override ConfigurationPropertyCollection Properties {
236             get {
237                 return _properties;
238             }
239         }
240
241         [ConfigurationProperty("mode", DefaultValue = SessionStateModule.MODE_DEFAULT)]
242         public SessionStateMode Mode {
243             get {
244                 return (SessionStateMode)base[_propMode];
245             }
246             set {
247                 base[_propMode] = value;
248             }
249         }
250
251         [ConfigurationProperty("stateConnectionString", DefaultValue = SessionStateModule.STATE_CONNECTION_STRING_DEFAULT)]
252         public string StateConnectionString {
253             get {
254                 return (string)base[_propStateConnectionString];
255             }
256             set {
257                 base[_propStateConnectionString] = value;
258             }
259         }
260
261         [ConfigurationProperty("stateNetworkTimeout", DefaultValue = "00:00:10")]
262         [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
263         public TimeSpan StateNetworkTimeout {
264             get {
265                 return (TimeSpan)base[_propStateNetworkTimeout];
266             }
267             set {
268                 base[_propStateNetworkTimeout] = value;
269             }
270         }
271
272         [ConfigurationProperty("sqlConnectionString", DefaultValue = SessionStateModule.SQL_CONNECTION_STRING_DEFAULT)]
273         public string SqlConnectionString {
274             get {
275                 return (string)base[_propSqlConnectionString];
276             }
277             set {
278                 base[_propSqlConnectionString] = value;
279             }
280         }
281
282         [ConfigurationProperty("sqlCommandTimeout", DefaultValue = "00:00:30")]
283         [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
284         public TimeSpan SqlCommandTimeout {
285             get {
286                 return (TimeSpan)base[_propSqlCommandTimeout];
287             }
288             set {
289                 base[_propSqlCommandTimeout] = value;
290             }
291         }
292
293         [ConfigurationProperty("sqlConnectionRetryInterval", DefaultValue = "00:00:00")]
294         [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
295         public TimeSpan SqlConnectionRetryInterval {
296             get {
297                 return (TimeSpan)base[_propSqlConnectionRetryInterval];
298             }
299             set {
300                 base[_propSqlConnectionRetryInterval] = value;
301             }
302         }
303
304
305         [ConfigurationProperty("customProvider", DefaultValue = "")]
306         public string CustomProvider {
307             get {
308                 return (string)base[_propCustomProvider];
309             }
310             set {
311                 base[_propCustomProvider] = value;
312             }
313         }
314
315         [ConfigurationProperty("cookieless")]
316         public HttpCookieMode Cookieless {
317             get {
318                 if (cookielessCached == false) {
319                     cookielessCache = ConvertToCookieMode((string)base[_propCookieless]);
320                     cookielessCached = true;
321                 }
322                 return cookielessCache;
323             }
324             set {
325                 base[_propCookieless] = value.ToString();
326                 cookielessCache = value;
327             }
328         }
329
330         [ConfigurationProperty("cookieName", DefaultValue = SessionIDManager.SESSION_COOKIE_DEFAULT)]
331         public string CookieName {
332             get {
333                 return (string)base[_propCookieName];
334             }
335             set {
336                 base[_propCookieName] = value;
337             }
338         }
339
340         [ConfigurationProperty("timeout", DefaultValue = "00:20:00")]
341         [TypeConverter(typeof(TimeSpanMinutesOrInfiniteConverter))]
342         [TimeSpanValidator(MinValueString = "00:01:00", MaxValueString = TimeSpanValidatorAttribute.TimeSpanMaxValue)]
343         public TimeSpan Timeout {
344             get {
345                 return (TimeSpan)base[_propTimeout];
346             }
347             set {
348                 base[_propTimeout] = value;
349             }
350         }
351
352         [ConfigurationProperty("allowCustomSqlDatabase", DefaultValue = false)]
353         public bool AllowCustomSqlDatabase {
354             get {
355                 return (bool)base[_propAllowCustomSqlDatabase];
356             }
357             set {
358                 base[_propAllowCustomSqlDatabase] = value;
359             }
360         }
361
362         [ConfigurationProperty("compressionEnabled", DefaultValue = false)]
363         public bool CompressionEnabled{
364             get {
365                 return (bool)base[_propCompressionEnabled];
366             }
367             set {
368                 base[_propCompressionEnabled] = value;
369             }
370         }
371
372         [ConfigurationProperty("regenerateExpiredSessionId", DefaultValue = true)]
373         public bool RegenerateExpiredSessionId {
374             get {
375                 if (regenerateExpiredSessionIdCached == false) {
376                     regenerateExpiredSessionIdCache = (bool)base[_propRegenerateExpiredSessionId];
377                     regenerateExpiredSessionIdCached = true;
378                 }
379                 return regenerateExpiredSessionIdCache;
380             }
381             set {
382                 base[_propRegenerateExpiredSessionId] = value;
383                 regenerateExpiredSessionIdCache = value;
384             }
385         }
386
387
388 #if DONTCOMPILE
389         [ConfigurationProperty("lockAttributes", DefaultValue = "")]
390         public string LockAttributes {
391             get {
392                 return (string)base[_propLockAttributes];
393             }
394             set {
395                 // base.LockedAttributes.SetFromList(value); // keep the internal list in sync
396                 base[_propLockAttributes] = value;
397             }
398         }
399 #endif
400
401         [ConfigurationProperty("providers")]
402         public ProviderSettingsCollection Providers {
403             get {
404                 return (ProviderSettingsCollection)base[_propProviders];
405             }
406         }
407
408         [ConfigurationProperty("partitionResolverType", DefaultValue = "")]
409         public string PartitionResolverType {
410             get {
411                 return (string)base[_propPartitionResolverType];
412             }
413             set {
414                 base[_propPartitionResolverType] = value;
415             }
416         }
417
418         [ConfigurationProperty("useHostingIdentity", DefaultValue = true)]
419         public bool UseHostingIdentity {
420             get {
421                 return (bool)base[_propUseHostingIdentity];
422             }
423             set {
424                 base[_propUseHostingIdentity] = value;
425             }
426         }
427
428         [ConfigurationProperty("sessionIDManagerType", DefaultValue = "")]
429         public string SessionIDManagerType {
430             get {
431                 return (string)base[_propSessionIDManagerType];
432             }
433             set {
434                 base[_propSessionIDManagerType] = value;
435             }
436         }
437
438
439         HttpCookieMode ConvertToCookieMode(string s) {
440             if (s == "true") {
441                 return HttpCookieMode.UseUri;
442             }
443             else if (s == "false") {
444                 return HttpCookieMode.UseCookies;
445             }
446             else {
447                 int iTemp = 0;
448                 Type enumType = typeof(HttpCookieMode);
449
450                 if (Enum.IsDefined(enumType, s)) {
451                     iTemp = (int)Enum.Parse(enumType, s);
452                 }
453                 else {
454                     // if not null and not defined throw error
455                     string names = "true, false";
456                     foreach (string name in Enum.GetNames(enumType)) {
457                         if (names == null) {
458                             names = name;
459                         }
460                         else {
461                             names += ", " + name;
462                         }
463                     }
464
465                     throw new ConfigurationErrorsException(
466                         SR.GetString(SR.Invalid_enum_attribute, "cookieless", names),
467                         ElementInformation.Properties["cookieless"].Source,
468                         ElementInformation.Properties["cookieless"].LineNumber);
469                 }
470
471                 return (HttpCookieMode)iTemp;
472             }
473         }
474
475         protected override void PostDeserialize() {
476             ConvertToCookieMode((string)base[_propCookieless]);
477         }
478
479         protected override ConfigurationElementProperty ElementProperty {
480             get {
481                 return s_elemProperty;
482             }
483         }
484
485         private static void Validate(object value) {
486             if (value == null) {
487                 throw new ArgumentNullException("sessionState");
488             }
489             Debug.Assert(value is SessionStateSection);
490
491             SessionStateSection elem = (SessionStateSection)value;
492
493             if (elem.Timeout.TotalMinutes > SessionStateModule.MAX_CACHE_BASED_TIMEOUT_MINUTES &&
494                 (elem.Mode == SessionStateMode.InProc ||
495                 elem.Mode == SessionStateMode.StateServer)) {
496                 throw new ConfigurationErrorsException(
497                     SR.GetString(SR.Invalid_cache_based_session_timeout),
498                     elem.ElementInformation.Properties["timeout"].Source,
499                     elem.ElementInformation.Properties["timeout"].LineNumber);
500             }
501         }
502     }
503 }