Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (111 loc) · 3.85 KB
/
Copy pathProgram.cs
File metadata and controls
131 lines (111 loc) · 3.85 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
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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using OfficeOpenXml;
using TestProject.Data;
using TestProject.Services;
using TestProject.Models;
using Microsoft.AspNetCore.Identity;
using System.Net.Mail;
using TestProject.Settings;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.SetBasePath(builder.Environment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
// Add database context
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Add Identity services
builder.Services.AddIdentity<User, IdentityRole<int>>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders()
.AddClaimsPrincipalFactory<CustomUserClaimsPrincipalFactory>(); // Register the custom claims factory
// Configure cookie authentication
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Login";
options.LogoutPath = "/Login?handler=Logout";
options.AccessDeniedPath = "/Login";
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
options.SlidingExpiration = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
options.Cookie.HttpOnly = true;
options.Cookie.MaxAge = TimeSpan.FromMinutes(30);
});
// Add authorization services
builder.Services.AddAuthorization();
// Add hosted service for seeding data
builder.Services.AddHostedService<SeedDataService>();
// Add Razor Pages services
builder.Services.AddRazorPages();
// Add Controllers support
builder.Services.AddControllers();
// Configure EmailSettings using the Options pattern
builder.Services.Configure<EmailSettings>(
builder.Configuration.GetSection("EmailSettings"));
// Register EmailSender (als dat nog niet gebeurd is)
// Kies de juiste lifetime: Transient is vaak goed voor SmtpClient
builder.Services.AddTransient<IEmailSender, EmailSender>();
// Add antiforgery services
builder.Services.AddAntiforgery(options =>
{
options.HeaderName = "X-CSRF-TOKEN";
});
// Set EPPlus license context
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
// Add logging services
builder.Services.AddLogging();
var app = builder.Build();
// Apply pending migrations
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
try
{
// Check if there are any pending migrations
if (context.Database.GetPendingMigrations().Any())
{
context.Database.Migrate();
app.Logger.LogInformation("Applied pending database migrations.");
}
else
{
app.Logger.LogInformation("No pending migrations to apply.");
}
}
catch (Exception ex)
{
app.Logger.LogError(ex, "An error occurred while migrating the database.");
throw;
}
}
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllers();
app.Run();