1 //------------------------------------------------------------------------------
2 // <copyright file="SessionStateSection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.Configuration {
10 using System.Configuration;
11 using System.Collections.Specialized;
12 using System.Collections;
13 using System.Globalization;
16 using System.ComponentModel;
17 using System.Web.SessionState;
18 using System.Diagnostics;
19 using System.Security.Permissions;
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]"
39 <providers> Custom store providers (class must inherit SessionStateStoreProviderBase)
41 name="string" Name to identify this provider instance by
42 type="string" Class that implements ISessionStateStore
43 provider-specific-configuration />
45 <remove Remove a provider
46 name="string" /> Name of provider to remove
47 <clear/> Remove all providers
52 stateConnectionString="tcpip=loopback:42424"
53 stateNetworkTimeout="10"
54 sqlConnectionString="data source=localhost;Integrated Security=SSPI"
55 sqlCommandTimeout="30"
58 allowCustomSqlDatabase="false"
59 compressionEnabled="false"
60 regenerateExpiredSessionId="false"
68 public sealed class SessionStateSection : ConfigurationSection {
69 private static readonly ConfigurationElementProperty s_elemProperty =
70 new ConfigurationElementProperty(new CallbackValidator(typeof(SessionStateSection), Validate));
73 private static ConfigurationPropertyCollection _properties;
75 private static readonly ConfigurationProperty _propMode =
76 new ConfigurationProperty("mode",
77 typeof(SessionStateMode),
78 SessionStateModule.MODE_DEFAULT,
79 ConfigurationPropertyOptions.None);
81 private static readonly ConfigurationProperty _propStateConnectionString =
82 new ConfigurationProperty("stateConnectionString",
84 SessionStateModule.STATE_CONNECTION_STRING_DEFAULT,
85 ConfigurationPropertyOptions.None);
87 private static readonly ConfigurationProperty _propStateNetworkTimeout =
88 new ConfigurationProperty("stateNetworkTimeout",
90 #if FEATURE_PAL // FEATURE_PAL does not enable OutOfProcSessionStore
91 TimeSpan.FromSeconds(600),
93 TimeSpan.FromSeconds((long)
94 OutOfProcSessionStateStore.STATE_NETWORK_TIMEOUT_DEFAULT),
96 StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
97 StdValidatorsAndConverters.PositiveTimeSpanValidator,
98 ConfigurationPropertyOptions.None);
100 private static readonly ConfigurationProperty _propSqlConnectionString =
101 new ConfigurationProperty("sqlConnectionString",
103 #if FEATURE_PAL // FEATURE_PAL does not enable SessionStateModule
104 "data source=localhost;Integrated Security=SSPI",
106 SessionStateModule.SQL_CONNECTION_STRING_DEFAULT,
107 #endif // FEATURE_PAL
108 ConfigurationPropertyOptions.None);
110 private static readonly ConfigurationProperty _propSqlCommandTimeout =
111 new ConfigurationProperty("sqlCommandTimeout",
113 #if FEATURE_PAL // FEATURE_PAL does not enable SqlSessionStateStore
114 TimeSpan.FromSeconds(1800),
116 TimeSpan.FromSeconds((long)
117 SqlSessionStateStore.SQL_COMMAND_TIMEOUT_DEFAULT),
118 #endif // FEATURE_PAL
119 StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
121 ConfigurationPropertyOptions.None);
123 private static readonly ConfigurationProperty _propSqlConnectionRetryInterval =
124 new ConfigurationProperty("sqlConnectionRetryInterval",
126 TimeSpan.FromSeconds(0),
127 StdValidatorsAndConverters.TimeSpanSecondsOrInfiniteConverter,
129 ConfigurationPropertyOptions.None);
131 private static readonly ConfigurationProperty _propCustomProvider =
132 new ConfigurationProperty("customProvider",
135 ConfigurationPropertyOptions.None);
137 private static readonly ConfigurationProperty _propCookieless =
138 new ConfigurationProperty("cookieless",
140 SessionIDManager.COOKIEMODE_DEFAULT.ToString(),
141 ConfigurationPropertyOptions.None);
143 private static readonly ConfigurationProperty _propCookieName =
144 new ConfigurationProperty("cookieName",
146 SessionIDManager.SESSION_COOKIE_DEFAULT,
147 ConfigurationPropertyOptions.None);
149 private static readonly ConfigurationProperty _propTimeout =
150 new ConfigurationProperty("timeout",
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",
160 ConfigurationPropertyOptions.None);
162 private static readonly ConfigurationProperty _propCompressionEnabled =
163 new ConfigurationProperty("compressionEnabled",
166 ConfigurationPropertyOptions.None);
169 // private static readonly ConfigurationProperty _propLockAttributes =
170 // new ConfigurationProperty("lockAttributes",
173 // ConfigurationPropertyOptions.None);
175 private static readonly ConfigurationProperty _propProviders =
176 new ConfigurationProperty("providers",
177 typeof(ProviderSettingsCollection),
179 ConfigurationPropertyOptions.None);
181 private static readonly ConfigurationProperty _propRegenerateExpiredSessionId =
182 new ConfigurationProperty("regenerateExpiredSessionId",
185 ConfigurationPropertyOptions.None);
187 private static readonly ConfigurationProperty _propPartitionResolverType =
188 new ConfigurationProperty("partitionResolverType",
191 ConfigurationPropertyOptions.None);
193 private static readonly ConfigurationProperty _propUseHostingIdentity =
194 new ConfigurationProperty("useHostingIdentity",
197 ConfigurationPropertyOptions.None);
199 private static readonly ConfigurationProperty _propSessionIDManagerType =
200 new ConfigurationProperty("sessionIDManagerType",
203 ConfigurationPropertyOptions.None);
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);
232 public SessionStateSection() {
235 protected override ConfigurationPropertyCollection Properties {
241 [ConfigurationProperty("mode", DefaultValue = SessionStateModule.MODE_DEFAULT)]
242 public SessionStateMode Mode {
244 return (SessionStateMode)base[_propMode];
247 base[_propMode] = value;
251 [ConfigurationProperty("stateConnectionString", DefaultValue = SessionStateModule.STATE_CONNECTION_STRING_DEFAULT)]
252 public string StateConnectionString {
254 return (string)base[_propStateConnectionString];
257 base[_propStateConnectionString] = value;
261 [ConfigurationProperty("stateNetworkTimeout", DefaultValue = "00:00:10")]
262 [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
263 public TimeSpan StateNetworkTimeout {
265 return (TimeSpan)base[_propStateNetworkTimeout];
268 base[_propStateNetworkTimeout] = value;
272 [ConfigurationProperty("sqlConnectionString", DefaultValue = SessionStateModule.SQL_CONNECTION_STRING_DEFAULT)]
273 public string SqlConnectionString {
275 return (string)base[_propSqlConnectionString];
278 base[_propSqlConnectionString] = value;
282 [ConfigurationProperty("sqlCommandTimeout", DefaultValue = "00:00:30")]
283 [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
284 public TimeSpan SqlCommandTimeout {
286 return (TimeSpan)base[_propSqlCommandTimeout];
289 base[_propSqlCommandTimeout] = value;
293 [ConfigurationProperty("sqlConnectionRetryInterval", DefaultValue = "00:00:00")]
294 [TypeConverter(typeof(TimeSpanSecondsOrInfiniteConverter))]
295 public TimeSpan SqlConnectionRetryInterval {
297 return (TimeSpan)base[_propSqlConnectionRetryInterval];
300 base[_propSqlConnectionRetryInterval] = value;
305 [ConfigurationProperty("customProvider", DefaultValue = "")]
306 public string CustomProvider {
308 return (string)base[_propCustomProvider];
311 base[_propCustomProvider] = value;
315 [ConfigurationProperty("cookieless")]
316 public HttpCookieMode Cookieless {
318 if (cookielessCached == false) {
319 cookielessCache = ConvertToCookieMode((string)base[_propCookieless]);
320 cookielessCached = true;
322 return cookielessCache;
325 base[_propCookieless] = value.ToString();
326 cookielessCache = value;
330 [ConfigurationProperty("cookieName", DefaultValue = SessionIDManager.SESSION_COOKIE_DEFAULT)]
331 public string CookieName {
333 return (string)base[_propCookieName];
336 base[_propCookieName] = value;
340 [ConfigurationProperty("timeout", DefaultValue = "00:20:00")]
341 [TypeConverter(typeof(TimeSpanMinutesOrInfiniteConverter))]
342 [TimeSpanValidator(MinValueString = "00:01:00", MaxValueString = TimeSpanValidatorAttribute.TimeSpanMaxValue)]
343 public TimeSpan Timeout {
345 return (TimeSpan)base[_propTimeout];
348 base[_propTimeout] = value;
352 [ConfigurationProperty("allowCustomSqlDatabase", DefaultValue = false)]
353 public bool AllowCustomSqlDatabase {
355 return (bool)base[_propAllowCustomSqlDatabase];
358 base[_propAllowCustomSqlDatabase] = value;
362 [ConfigurationProperty("compressionEnabled", DefaultValue = false)]
363 public bool CompressionEnabled{
365 return (bool)base[_propCompressionEnabled];
368 base[_propCompressionEnabled] = value;
372 [ConfigurationProperty("regenerateExpiredSessionId", DefaultValue = true)]
373 public bool RegenerateExpiredSessionId {
375 if (regenerateExpiredSessionIdCached == false) {
376 regenerateExpiredSessionIdCache = (bool)base[_propRegenerateExpiredSessionId];
377 regenerateExpiredSessionIdCached = true;
379 return regenerateExpiredSessionIdCache;
382 base[_propRegenerateExpiredSessionId] = value;
383 regenerateExpiredSessionIdCache = value;
389 [ConfigurationProperty("lockAttributes", DefaultValue = "")]
390 public string LockAttributes {
392 return (string)base[_propLockAttributes];
395 // base.LockedAttributes.SetFromList(value); // keep the internal list in sync
396 base[_propLockAttributes] = value;
401 [ConfigurationProperty("providers")]
402 public ProviderSettingsCollection Providers {
404 return (ProviderSettingsCollection)base[_propProviders];
408 [ConfigurationProperty("partitionResolverType", DefaultValue = "")]
409 public string PartitionResolverType {
411 return (string)base[_propPartitionResolverType];
414 base[_propPartitionResolverType] = value;
418 [ConfigurationProperty("useHostingIdentity", DefaultValue = true)]
419 public bool UseHostingIdentity {
421 return (bool)base[_propUseHostingIdentity];
424 base[_propUseHostingIdentity] = value;
428 [ConfigurationProperty("sessionIDManagerType", DefaultValue = "")]
429 public string SessionIDManagerType {
431 return (string)base[_propSessionIDManagerType];
434 base[_propSessionIDManagerType] = value;
439 HttpCookieMode ConvertToCookieMode(string s) {
441 return HttpCookieMode.UseUri;
443 else if (s == "false") {
444 return HttpCookieMode.UseCookies;
448 Type enumType = typeof(HttpCookieMode);
450 if (Enum.IsDefined(enumType, s)) {
451 iTemp = (int)Enum.Parse(enumType, s);
454 // if not null and not defined throw error
455 string names = "true, false";
456 foreach (string name in Enum.GetNames(enumType)) {
461 names += ", " + name;
465 throw new ConfigurationErrorsException(
466 SR.GetString(SR.Invalid_enum_attribute, "cookieless", names),
467 ElementInformation.Properties["cookieless"].Source,
468 ElementInformation.Properties["cookieless"].LineNumber);
471 return (HttpCookieMode)iTemp;
475 protected override void PostDeserialize() {
476 ConvertToCookieMode((string)base[_propCookieless]);
479 protected override ConfigurationElementProperty ElementProperty {
481 return s_elemProperty;
485 private static void Validate(object value) {
487 throw new ArgumentNullException("sessionState");
489 Debug.Assert(value is SessionStateSection);
491 SessionStateSection elem = (SessionStateSection)value;
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);