Modification du Progam
This commit is contained in:
50
Program.cs
50
Program.cs
@@ -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 l’authentification 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user