-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
327 lines (298 loc) · 13.4 KB
/
Startup.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Mail;
using System.Threading.Tasks;
using Azure.Identity;
using CorrelationId;
using CorrelationId.DependencyInjection;
using GDB.App.Controllers.Frontend;
using GDB.App.Controllers.General.Utility;
using GDB.App.ErrorHandling;
using GDB.App.HealthChecks;
using GDB.App.Security;
using GDB.App.StartupConfiguration;
using GDB.App.StartupConfiguration.Settings;
using GDB.Common.Persistence;
using GDB.Common.Settings;
using GDB.EmailSending;
using GDB.Persistence;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.CookiePolicy;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Net.Http.Headers;
namespace GDB.App
{
public class Startup
{
private IConfiguration _configuration;
private IWebHostEnvironment _environment;
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
{
_configuration = configuration;
_environment = environment;
}
public void ConfigureServices(IServiceCollection services)
{
// Configurations
services.Configure<AddressSettings>(_configuration.GetSection("Addresses"));
services.Configure<DataProtectionSettings>(_configuration.GetSection("DataProtection"));
services.Configure<SentryOptions>(_configuration.GetSection("Sentry"));
services.Configure<StorageSettings>(_configuration.GetSection("Storage"));
services.AddScoped<DatabaseConnectionSettings>((services) =>
{
return new DatabaseConnectionSettings() { Database = _configuration.GetConnectionString("Database") };
});
// Data
services.AddScoped<IPersistence, DapperPersistence>();
// Business/Domain Logic
BusinessServiceConfiguration.Configure(services);
// data protection
if (!_environment.IsDevelopment())
{
services.AddDataProtection()
.PersistKeysToAzureBlobStorage(_configuration["Storage:ConnectionString"], _configuration["DataProtection:StorageKeyContainer"], _configuration["DataProtection:StorageKeyBlob"])
.ProtectKeysWithAzureKeyVault(new Uri(_configuration["DataProtection:KVKeyIdentifier"]), new DefaultAzureCredential());
}
// interactive security
services.AddAntiforgery();
services.AddAuthentication(SecurityConstants.CookieAuthScheme)
.AddCookie(SecurityConstants.CookieAuthScheme, options =>
{
options.LoginPath = "/account/login";
options.AccessDeniedPath = "/account/accessdenied";
options.LogoutPath = "/account/logout";
});
services.AddScoped<IAccountCookies, AccountCookies>();
// Policies
{
// Cookie policies
services.Configure<CookiePolicyOptions>(options =>
{
// downgrade samesite for local development to prevent warnings cluttering up console when debugging on HTTP, etc
options.MinimumSameSitePolicy = _environment.IsDevelopment()
? Microsoft.AspNetCore.Http.SameSiteMode.Lax
: Microsoft.AspNetCore.Http.SameSiteMode.Strict;
options.HttpOnly = HttpOnlyPolicy.None;
options.Secure = CookieSecurePolicy.Always;
});
// Authorizations policies
services.AddAuthorization(options =>
{
options.AddPolicy(Policies.InteractiveUserAccess, builder =>
{
builder.RequireAuthenticatedUser();
builder.AuthenticationSchemes.Add(SecurityConstants.CookieAuthScheme);
builder.RequireClaim(ClaimNames.SessionId);
builder.RequireClaim(ClaimNames.UserId);
builder.RequireClaim(ClaimNames.UserName);
});
options.DefaultPolicy = options.GetPolicy(Policies.InteractiveUserAccess);
});
// CORS policies
services.AddCors(options =>
{
options.AddPolicy(SecurityConstants.CORS_AllowAny, builder =>
{
builder.AllowAnyOrigin();
});
});
}
// Endpoints
{
services.AddDefaultCorrelationId();
// Health
var healthChecks = services.AddHealthChecks()
.AddCheck<DatabaseHealthCheck>("database")
.AddCheck<StorageHealthCheck>("storage");
if (!_environment.IsDevelopment())
{
healthChecks.AddCheck<DataProtectionKeysHealthCheck>("dataprotection");
}
// MVC
var builder = services.AddControllersWithViews(options =>
{
options.Filters.Add(new UnhandledApiExceptionFilter(new string[] {
"/api/fe"
}));
});
if (_environment.IsDevelopment())
{
builder.AddRazorRuntimeCompilation();
}
services.Configure<RazorViewEngineOptions>(o =>
{
// {2} is area, {1} is controller,{0} is the action
o.ViewLocationFormats.Clear();
o.ViewLocationFormats.Add("/Controllers/{1}/Views/{0}" + RazorViewEngine.ViewExtension);
o.AreaViewLocationFormats.Add("/Controllers/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
});
// SignalR
services.AddSignalR();
// SPA
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
// Compression
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<GzipCompressionProvider>();
options.Providers.Add<BrotliCompressionProvider>();
});
//email notification service
services.AddScoped<IEmailSender, EmailSender>();
if (_configuration["Email:Method"].Equals("File"))
{
services.AddFluentEmail(_configuration["Email:FromAddress"], _configuration["Email:FromName"])
.AddRazorRenderer(typeof(EmailSender))
.AddSmtpSender(new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory,
PickupDirectoryLocation = Path.Combine(_environment.ContentRootPath, _configuration["Email:FilePath"])
});
}
else
{
services.AddFluentEmail(_configuration["Email:FromAddress"], _configuration["Email:FromName"])
.AddRazorRenderer(typeof(EmailSender))
.AddSmtpSender(new SmtpClient(_configuration["Email:Host"], int.Parse(_configuration["Email:Port"])) {
EnableSsl = true,
Credentials = new NetworkCredential(_configuration["Email:Username"], _configuration["Email:Password"])
});
}
// Monitoring
services.AddApplicationInsightsTelemetry();
// SignalR Sender
services.AddScoped<ISignalRSender, SignalRSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// -- Development tasks
if (env.IsDevelopment())
{
LocalDevelopmentTasks.MigrateDatabase(_configuration.GetConnectionString("Database"));
}
// -- Continue configuration
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/error");
app.UseHsts();
// only use HTTPS non-locally as livereload/ws proxying to node doesn't work w/ HTTPS
app.UseHttpsRedirection();
}
app.UseResponseCompression();
app.UseStaticFiles();
app.UseRouting();
app.UseCorrelationId();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors();
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
ResponseWriter = HealthCheckResponse.WriteResponse
})
.RequireCors(SecurityConstants.CORS_AllowAny);
endpoints.MapHub<SignalRHub>("/api/fe/hub");
endpoints.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
});
// Client SPA only from here forwards
{
// I haven't figured out how to apply a security policy
// from the Startup, so we'll do the default challenge and
// that does the right thing for now
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
if (context.Request.Path.StartsWithSegments("/base.css") ||
context.Request.Path.StartsWithSegments("/fonts") ||
context.Request.Path.StartsWithSegments("/CircleArrow56.png") ||
(env.IsDevelopment() && context.Request.Path.Value.EndsWith(".js.map")))
{
await next();
}
else
{
await context.ChallengeAsync();
}
}
else
{
await next();
}
});
app.UseSpaStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = ctx =>
{
if (ctx.Context.Request.Path.StartsWithSegments("/static"))
{
// Cache all static resources for 1 year (versioned filenames)
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(365)
};
}
else
{
// Do not cache explicit `/index.html` or any other files. See also: `DefaultPageStaticFileOptions` below for implicit "/index.html"
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(0)
};
}
}
});
app.UseSpa(spa =>
{
spa.Options.DefaultPage = "/index.html";
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions()
{
OnPrepareResponse = ctx =>
{
// Do not cache implicit `/index.html`. See also: `UseSpaStaticFiles` above
var headers = ctx.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
Public = true,
MaxAge = TimeSpan.FromDays(0)
};
}
};
if (env.IsDevelopment())
{
spa.Options.DefaultPage = "/index.html";
spa.Options.StartupTimeout = TimeSpan.FromSeconds(120);
LocalDevelopmentTasks.StartFrontendService(spa, "../../frontend", "yarn", (port) => $"run dev", "Your application is ready");
}
});
}
}
}
}