-
Notifications
You must be signed in to change notification settings - Fork 112
/
Startup.cs
168 lines (144 loc) · 6.17 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
namespace BankSystem.Web
{
using System;
using AutoMapper;
using BankSystem.Models;
using Common.AutoMapping.Profiles;
using Common.Configuration;
using Common.EmailSender;
using Common.Utils;
using Data;
using Infrastructure.Extensions;
using Infrastructure.Middleware;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Net.Http.Headers;
using SameSiteMode = Microsoft.AspNetCore.Http.SameSiteMode;
public class Startup
{
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContextPool<BankSystemDbContext>(options =>
options.UseSqlServer(
this.Configuration.GetConnectionString("DefaultConnection")));
services
.Configure<CookieTempDataProviderOptions>(options => { options.Cookie.IsEssential = true; });
services.AddIdentity<BankUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedEmail = true;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
})
.AddEntityFrameworkStores<BankSystemDbContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.SlidingExpiration = true;
options.LoginPath = "/account/login";
options.LogoutPath = "/account/logout";
});
services
.AddDomainServices()
.AddApplicationServices()
.AddCommonProjectServices()
.AddAuthentication();
services.Configure<SecurityStampValidatorOptions>(options =>
{
options.ValidationInterval = TimeSpan.Zero;
});
services
.Configure<RouteOptions>(options => options.LowercaseUrls = true);
services
.Configure<BankConfiguration>(
this.Configuration.GetSection(nameof(BankConfiguration)))
.Configure<SendGridConfiguration>(
this.Configuration.GetSection(nameof(SendGridConfiguration)));
services
.PostConfigure<BankConfiguration>(settings =>
{
if (!ValidationUtil.IsObjectValid(settings))
{
throw new ApplicationException("BankConfiguration is invalid");
}
})
.PostConfigure<SendGridConfiguration>(settings =>
{
if (!ValidationUtil.IsObjectValid(settings))
{
throw new ApplicationException("SendGridConfiguration is invalid");
}
});
services
.AddResponseCompression(options => options.EnableForHttps = true);
services.AddMvc(options => { options.Filters.Add<AutoValidateAntiforgeryTokenAttribute>(); })
.AddRazorPagesOptions(options => { options.Conventions.AuthorizePage("/MoneyTransfers"); })
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Mapper.Initialize(config => config.AddProfile<DefaultProfile>());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/error/500");
app.UseHsts();
}
// We can add all recommended headers or add custom ones or choose between different ones
app.AddDefaultSecurityHeaders(
new SecurityHeadersBuilder()
.AddDefaultSecurePolicy());
app.UseResponseCompression();
app.UseStatusCodePages();
app.UseHttpsRedirection();
app.UseStaticFiles(
new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int cacheDurationInSeconds = 60 * 60 * 24 * 365; // 1 year
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
$"public,max-age={cacheDurationInSeconds}";
}
});
app.UseCookiePolicy();
app.UseAuthentication();
app.UseStatusCodePagesWithReExecute("/error/{0}");
app.UseMvc(routes =>
{
routes.MapRoute(
"areas",
"{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
app.InitializeDatabase();
}
}
}