-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (73 loc) · 2.57 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (73 loc) · 2.57 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
using FormulaireSOLID.Filters;
using Microsoft.AspNetCore.Http.Features;
var builder = WebApplication.CreateBuilder(args);
// Configuration des services
builder.Services.AddControllersWithViews(options =>
{
// Filtres globaux
options.Filters.Add(typeof(ThemeFilter)); // Applique le thème à toutes les vues
});
// Configuration des sessions
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(2); // Durée d'inactivité
options.Cookie.HttpOnly = true; // Protection XSS
options.Cookie.SecurePolicy = CookieSecurePolicy.Always; // HTTPS seulement
options.Cookie.SameSite = SameSiteMode.Strict; // Protection CSRF
options.Cookie.IsEssential = true; // Cookie essentiel
options.Cookie.MaxAge = TimeSpan.FromHours(24); // Durée absolue
});
// Configuration de la taille maximale des fichiers pour les uploads
builder.Services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue;
});
// Logging
builder.Services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
logging.AddEventLog();
});
var app = builder.Build();
// Configuration du pipeline HTTP
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts(); // HTTP Strict Transport Security
}
app.UseHttpsRedirection(); // Redirection HTTPS forcée
app.UseStaticFiles(); // Fichiers statiques (wwwroot)
app.UseRouting(); // Routage
app.UseAuthorization(); // Autorisation
// Middleware de session (doit être après UseRouting et avant UseEndpoints)
app.UseSession();
// Pipeline personnalisé (exemple d'ajout de middleware global)
app.Use(async (context, next) =>
{
// Logging global des requêtes
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
logger.LogInformation($"Requête: {context.Request.Method} {context.Request.Path}");
await next();
logger.LogInformation($"Réponse: {context.Response.StatusCode}");
});
// Routes par défaut
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Routes personnalisées
app.MapControllerRoute(
name: "user",
pattern: "compte/{action=Login}",
defaults: new { controller = "User" });
app.MapControllerRoute(
name: "products",
pattern: "produits/{action=Index}/{id?}",
defaults: new { controller = "Product" });
app.MapControllerRoute(
name: "productCRUD",
pattern: "produits/{action}/{id?}",
defaults: new { controller = "Product" });
app.Run();