forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreApplicationStartCode.cs
More file actions
100 lines (85 loc) · 4.74 KB
/
PreApplicationStartCode.cs
File metadata and controls
100 lines (85 loc) · 4.74 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 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.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.WebPages;
using System.Web.WebPages.Razor;
using WebMatrix.Data;
namespace WebMatrix.WebData
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class PreApplicationStartCode
{
// NOTE: Do not add public fields, methods, or other members to this class.
// This class does not show up in Intellisense so members on it will not be
// discoverable by users. Place new members on more appropriate classes that
// relate to the public API (for example, a LoginUrl property should go on a
// membership-related class).
private const string LoginUrlKey = "loginUrl";
private static bool _startWasCalled;
public static void Start()
{
// Even though ASP.NET will only call each PreAppStart once, we sometimes internally call one PreAppStart from
// another PreAppStart to ensure that things get initialized in the right order. ASP.NET does not guarantee the
// order so we have to guard against multiple calls.
// All Start calls are made on same thread, so no lock needed here.
if (_startWasCalled)
{
return;
}
_startWasCalled = true;
// Summary of Simple Membership startup behavior:
// 1. If the appSetting enabledSimpleMembership is present and equal to "false", NEITHER SimpleMembership NOR AutoFormsAuth are activated
// 2. If the appSetting is true, a non-boolean string or not present, BOTH may be activated
// a. SimpleMembership ONLY replaces the AspNetSqlMemberhipProvider, but it does replace it even if it isn't the default. This
// means that anything accessing this provider by name will get Simple Membership, but if this provider is no longer the default
// then SimpleMembership does not affect the default
// b. SimpleMembership delegates to the previous default provider UNLESS WebSecurity.InitializeDatabaseConnection is called.
// Initialize membership provider
WebSecurity.PreAppStartInit();
// Initialize Forms Authentication default configuration
SetUpFormsAuthentication();
// Wire up WebMatrix.Data's Database object to the ASP.NET Web Pages resource tracker
Database.ConnectionOpened += OnConnectionOpened;
// Auto import the WebMatrix.Data and WebMatrix.WebData namespaces to all apps that are executing.
WebPageRazorHost.AddGlobalImport("WebMatrix.Data");
WebPageRazorHost.AddGlobalImport("WebMatrix.WebData");
}
private static void OnConnectionOpened(object sender, ConnectionEventArgs e)
{
// Register all open connections for disposing at the end of the request
HttpContext httpContext = HttpContext.Current;
if (httpContext != null)
{
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(httpContext);
httpContextWrapper.RegisterForDispose(e.Connection);
}
}
private static void SetUpFormsAuthentication()
{
if (ConfigUtil.SimpleMembershipEnabled)
{
NameValueCollection configurationData = new NameValueCollection();
string appSettingsLoginUrl = ConfigurationManager.AppSettings[FormsAuthenticationSettings.LoginUrlKey];
if (appSettingsLoginUrl != null)
{
// Allow use of <add key="loginUrl" value="~/MyPath/LogOn" /> as a shortcut to specify
// a custom log in url
configurationData[LoginUrlKey] = appSettingsLoginUrl;
}
else if (!ConfigUtil.ShouldPreserveLoginUrl())
{
// Otherwise, use the default login url, but only if PreserveLoginUrl != true
// If PreserveLoginUrl == true, we do not want to override FormsAuthentication's default
// behavior because trying to evaluate FormsAuthentication.LoginUrl at this point in a
// PreAppStart method would cause app-relative URLs to be evaluated incorrectly.
configurationData[LoginUrlKey] = FormsAuthenticationSettings.DefaultLoginUrl;
}
FormsAuthentication.EnableFormsAuthentication(configurationData);
}
}
}
}