Modification du Progam

This commit is contained in:
2025-08-03 17:49:49 +02:00
parent 9a1159d4bc
commit bcef0a472b

View File

@@ -3,7 +3,6 @@ using administration.Services;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using static DBConnectionController;
namespace administration namespace administration
{ {
@@ -15,48 +14,66 @@ namespace administration
DotNetEnv.Env.Load(); DotNetEnv.Env.Load();
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// ==============================================
// 1⃣ Configurer la base de données
// ==============================================
var dbConnection = Environment.GetEnvironmentVariable("ADMIN_DB_CONNECTION");
if (string.IsNullOrEmpty(dbConnection))
throw new Exception("❌ ADMIN_DB_CONNECTION est introuvable dans les variables d'environnement.");
builder.Services.AddDbContext<FinancesContext>(options => builder.Services.AddDbContext<FinancesContext>(options =>
options.UseSqlServer( options.UseSqlServer(dbConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())
Environment.GetEnvironmentVariable("ADMIN_DB_CONNECTION"),
sqlOptions => sqlOptions.EnableRetryOnFailure()
)
); );
AppSettings.Initialize(builder.Configuration); AppSettings.Initialize(builder.Configuration);
// ==============================================
// 2⃣ Ajouter la session
// ==============================================
builder.Services.AddSession(options => builder.Services.AddSession(options =>
{ {
options.IdleTimeout = TimeSpan.FromMinutes(300); // Durée d'expiration options.IdleTimeout = TimeSpan.FromMinutes(300); // Expiration session
options.Cookie.HttpOnly = true; options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true; options.Cookie.IsEssential = true;
}); });
// ==============================================
// 3⃣ Ajouter lauthentification Basic
// ==============================================
builder.Services.AddAuthentication("BasicAuthentication") builder.Services.AddAuthentication("BasicAuthentication")
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null); .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
builder.Services.AddAuthorization(); builder.Services.AddAuthorization();
// Ajouter les services MVC // ==============================================
// 4⃣ Ajouter MVC + dépendances
// ==============================================
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpContextAccessor(); // obligatoire
builder.Services.AddScoped<IUserSessionService, UserSessionService>(); builder.Services.AddScoped<IUserSessionService, UserSessionService>();
// ==============================================
// Configurer la politique CORS pour autoriser l'accès depuis le frontend local // 5⃣ CORS pour le frontend
// ==============================================
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy("AllowFrontend", policy => options.AddPolicy("AllowFrontend", policy =>
{ {
policy.WithOrigins("http://localhost:5018") policy.WithOrigins("http://localhost:5018", "https://administration.byakurepo.online")
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod(); .AllowAnyMethod();
}); });
}); });
// ==============================================
// 6⃣ Construire l'application
// ==============================================
var app = builder.Build(); var app = builder.Build();
// Configuration du pipeline HTTP // ==============================================
// 7⃣ Middleware Pipeline
// ==============================================
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
@@ -73,12 +90,13 @@ namespace administration
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); app.UseAuthorization();
// Route par défaut // ==============================================
// 8⃣ Routes
// ==============================================
app.MapControllerRoute( app.MapControllerRoute(
name: "default", name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"); pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run(); app.Run();
} }
} }