104 lines
3.9 KiB
C#
104 lines
3.9 KiB
C#
using administration.Models;
|
||
using administration.Services;
|
||
using Microsoft.AspNetCore.Authentication;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.EntityFrameworkCore;
|
||
|
||
namespace administration
|
||
{
|
||
public class Program
|
||
{
|
||
public static void Main(string[] args)
|
||
{
|
||
// Charger les variables d'environnement depuis .env
|
||
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(dbConnection, sqlOptions => sqlOptions.EnableRetryOnFailure())
|
||
);
|
||
|
||
AppSettings.Initialize(builder.Configuration);
|
||
|
||
// ==============================================
|
||
// 2️⃣ Ajouter la session
|
||
// ==============================================
|
||
builder.Services.AddSession(options =>
|
||
{
|
||
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();
|
||
|
||
// ==============================================
|
||
// 4️⃣ Ajouter MVC + dépendances
|
||
// ==============================================
|
||
builder.Services.AddControllersWithViews();
|
||
builder.Services.AddHttpContextAccessor();
|
||
builder.Services.AddScoped<IUserSessionService, UserSessionService>();
|
||
|
||
// ==============================================
|
||
// 5️⃣ CORS pour le frontend
|
||
// ==============================================
|
||
builder.Services.AddCors(options =>
|
||
{
|
||
options.AddPolicy("AllowFrontend", policy =>
|
||
{
|
||
policy.WithOrigins("http://localhost:5018", "https://administration.byakurepo.online")
|
||
.AllowAnyHeader()
|
||
.AllowAnyMethod();
|
||
});
|
||
});
|
||
|
||
// ==============================================
|
||
// 6️⃣ Construire l'application
|
||
// ==============================================
|
||
var app = builder.Build();
|
||
|
||
// ==============================================
|
||
// 7️⃣ Middleware Pipeline
|
||
// ==============================================
|
||
if (!app.Environment.IsDevelopment())
|
||
{
|
||
app.UseExceptionHandler("/Home/Error");
|
||
app.UseHsts();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseStaticFiles();
|
||
|
||
app.UseRouting();
|
||
app.UseCors("AllowFrontend");
|
||
app.UseSession();
|
||
|
||
app.UseAuthentication();
|
||
app.UseAuthorization();
|
||
|
||
// ==============================================
|
||
// 8️⃣ Routes
|
||
// ==============================================
|
||
app.MapControllerRoute(
|
||
name: "default",
|
||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||
|
||
app.Run();
|
||
}
|
||
}
|
||
}
|