Modification du Progam
This commit is contained in:
48
Program.cs
48
Program.cs
@@ -3,7 +3,6 @@ using administration.Services;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using static DBConnectionController;
|
||||
|
||||
namespace administration
|
||||
{
|
||||
@@ -15,48 +14,66 @@ namespace administration
|
||||
DotNetEnv.Env.Load();
|
||||
|
||||
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 =>
|
||||
options.UseSqlServer(
|
||||
Environment.GetEnvironmentVariable("ADMIN_DB_CONNECTION"),
|
||||
sqlOptions => sqlOptions.EnableRetryOnFailure()
|
||||
)
|
||||
options.UseSqlServer(dbConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())
|
||||
);
|
||||
|
||||
AppSettings.Initialize(builder.Configuration);
|
||||
|
||||
// ==============================================
|
||||
// 2️⃣ Ajouter la session
|
||||
// ==============================================
|
||||
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.IsEssential = true;
|
||||
});
|
||||
|
||||
// ==============================================
|
||||
// 3️⃣ Ajouter l’authentification Basic
|
||||
// ==============================================
|
||||
builder.Services.AddAuthentication("BasicAuthentication")
|
||||
.AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
// Ajouter les services MVC
|
||||
// ==============================================
|
||||
// 4️⃣ Ajouter MVC + dépendances
|
||||
// ==============================================
|
||||
builder.Services.AddControllersWithViews();
|
||||
|
||||
builder.Services.AddHttpContextAccessor(); // obligatoire
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
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 =>
|
||||
{
|
||||
options.AddPolicy("AllowFrontend", policy =>
|
||||
{
|
||||
policy.WithOrigins("http://localhost:5018")
|
||||
policy.WithOrigins("http://localhost:5018", "https://administration.byakurepo.online")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
|
||||
// ==============================================
|
||||
// 6️⃣ Construire l'application
|
||||
// ==============================================
|
||||
var app = builder.Build();
|
||||
|
||||
// Configuration du pipeline HTTP
|
||||
// ==============================================
|
||||
// 7️⃣ Middleware Pipeline
|
||||
// ==============================================
|
||||
if (!app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
@@ -73,12 +90,13 @@ namespace administration
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
// Route par défaut
|
||||
// ==============================================
|
||||
// 8️⃣ Routes
|
||||
// ==============================================
|
||||
app.MapControllerRoute(
|
||||
name: "default",
|
||||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||||
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user