-
Notifications
You must be signed in to change notification settings - Fork 1
/
Global.asax.cs
258 lines (223 loc) · 7.74 KB
/
Global.asax.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Global.asax.cs" company="Copyright ©2014 John Allberg & Jonas Fredriksson">
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// <summary>
// Defines the Global type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace WinShooter
{
using System;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.Security;
using log4net;
using log4net.Config;
using WinShooter.Logic.Authentication;
using WinShooter.Web.DatabaseMigrations;
/// <summary>
/// The global.
/// </summary>
public class Global : HttpApplication
{
/// <summary>
/// The log.
/// </summary>
private readonly ILog log;
/// <summary>
/// Initializes a new instance of the <see cref="Global"/> class.
/// </summary>
public Global()
{
this.log = LogManager.GetLogger(this.GetType());
this.GoogleTrustCertificateFetcher = new GoogleTrustCertificateFetcher();
}
/// <summary>
/// Gets the Google trust certificate fetcher.
/// </summary>
public GoogleTrustCertificateFetcher GoogleTrustCertificateFetcher { get; private set; }
/// <summary>
/// Check if a path is a file path
/// </summary>
/// <param name="path">
/// The path.
/// </param>
/// <returns>
/// True if it is a file, meaning it has an extension
/// </returns>
public static bool IsFile(string path)
{
var regex = new Regex("^.*\\.[\\w]*$");
return regex.IsMatch(path);
}
/// <summary>
/// Check if a path is a API call
/// </summary>
/// <param name="path">
/// The path.
/// </param>
/// <returns>
/// True if it is a API call.
/// </returns>
public static bool IsApi(string path)
{
var regex = new Regex("^/api/", RegexOptions.IgnoreCase);
return regex.IsMatch(path);
}
/// <summary>
/// Check if a path is a account call
/// </summary>
/// <param name="path">
/// The path.
/// </param>
/// <returns>
/// True if it is a account call.
/// </returns>
public static bool IsAccount(string path)
{
var regex = new Regex("^/account/", RegexOptions.IgnoreCase);
return regex.IsMatch(path);
}
/// <summary>
/// The application start, where everything is setup.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
protected void Application_Start(object sender, EventArgs e)
{
XmlConfigurator.Configure();
this.log.Info("Winshooter web starting.");
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var sqlDatabaseMigrator = new SqlDatabaseMigrator();
sqlDatabaseMigrator.MigrateToLatest(ConfigurationManager.ConnectionStrings["WinShooterConnection"].ConnectionString);
this.log.Info("Winshooter web startup complete.");
}
/// <summary>
/// The application post authenticate request handler.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
var authCookie = this.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null)
{
return;
}
var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
if (authTicket == null || authTicket.UserData == null || authTicket.UserData == string.Empty)
{
return;
}
var serializer = new JavaScriptSerializer();
var serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
var newUser = new CustomPrincipal(authTicket.Name)
{
UserId = serializeModel.UserId,
IsSystemAdmin = serializeModel.IsSystemAdmin,
FirstName = serializeModel.FirstName,
LastName = serializeModel.LastName
};
HttpContext.Current.User = newUser;
}
/// <summary>
/// Code that runs with unhandled exceptions.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
protected void Application_Error(object sender, EventArgs e)
{
// Get the exception object.
var exc = this.Server.GetLastError();
this.log.Error("Unexpected error: " + exc);
}
/// <summary>
/// The application begin request.
/// </summary>
/// <param name="sender">
/// The sender.
/// </param>
/// <param name="e">
/// The e.
/// </param>
protected void Application_BeginRequest(object sender, EventArgs e)
{
// For each call, validate if it's an api or file call.
// If it is, let the call through.
// If it's a call to a path, it's supposed to go to
// Angular, which is on the root so we rewrite the path
// internally
var path = HttpContext.Current.Request.Path;
if (IsStrangeDebuggerCall(path))
{
return;
}
if (IsAccount(path))
{
return;
}
if (IsApi(path))
{
return;
}
if (IsFile(path))
{
return;
}
if (path.ToUpper() == "/JSERRORLOGGER")
{
return;
}
this.Context.RewritePath("/");
}
/// <summary>
/// Check if a path is a strange debugger call.
/// </summary>
/// <param name="path">
/// The path.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
private static bool IsStrangeDebuggerCall(string path)
{
if (path.StartsWith("/__browser"))
{
return true;
}
return false;
}
}
}