forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionStateUtil.cs
More file actions
84 lines (75 loc) · 3.68 KB
/
SessionStateUtil.cs
File metadata and controls
84 lines (75 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Web.Razor;
using System.Web.SessionState;
using System.Web.WebPages.Resources;
namespace System.Web.WebPages
{
internal static class SessionStateUtil
{
private static readonly ConcurrentDictionary<Type, SessionStateBehavior?> _sessionStateBehaviorCache = new ConcurrentDictionary<Type, SessionStateBehavior?>();
internal static void SetUpSessionState(HttpContextBase context, IHttpHandler handler)
{
SetUpSessionState(context, handler, _sessionStateBehaviorCache);
}
internal static void SetUpSessionState(HttpContextBase context, IHttpHandler handler, ConcurrentDictionary<Type, SessionStateBehavior?> cache)
{
WebPageHttpHandler webPageHandler = handler as WebPageHttpHandler;
Debug.Assert(handler != null);
SessionStateBehavior? sessionState = GetSessionStateBehavior(webPageHandler.RequestedPage, cache);
if (sessionState != null)
{
// If the page explicitly specifies a session state value, return since it has the most priority.
context.SetSessionStateBehavior(sessionState.Value);
return;
}
WebPageRenderingBase page = webPageHandler.StartPage;
StartPage startPage = null;
do
{
// Drill down _AppStart and _PageStart.
startPage = page as StartPage;
if (startPage != null)
{
sessionState = GetSessionStateBehavior(page, cache);
page = startPage.ChildPage;
}
}
while (startPage != null);
if (sessionState != null)
{
context.SetSessionStateBehavior(sessionState.Value);
}
}
private static SessionStateBehavior? GetSessionStateBehavior(WebPageExecutingBase page, ConcurrentDictionary<Type, SessionStateBehavior?> cache)
{
return cache.GetOrAdd(page.GetType(), type =>
{
SessionStateBehavior sessionStateBehavior = SessionStateBehavior.Default;
var attributes = (RazorDirectiveAttribute[])type.GetCustomAttributes(typeof(RazorDirectiveAttribute), inherit: false);
var directiveAttributes = attributes.Where(attr => StringComparer.OrdinalIgnoreCase.Equals("sessionstate", attr.Name))
.ToList();
if (!directiveAttributes.Any())
{
return null;
}
if (directiveAttributes.Count > 1)
{
throw new InvalidOperationException(WebPageResources.SessionState_TooManyValues);
}
var directiveAttribute = directiveAttributes[0];
if (!Enum.TryParse<SessionStateBehavior>(directiveAttribute.Value, ignoreCase: true, result: out sessionStateBehavior))
{
var values = Enum.GetValues(typeof(SessionStateBehavior)).Cast<SessionStateBehavior>().Select(s => s.ToString());
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, WebPageResources.SessionState_InvalidValue,
directiveAttribute.Value, page.VirtualPath, String.Join(", ", values)));
}
return sessionStateBehavior;
});
}
}
}