forked from Joseph5712/ClientReportManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
69 lines (53 loc) · 2.47 KB
/
Copy pathProgram.cs
File metadata and controls
69 lines (53 loc) · 2.47 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
using ClientReportManager.Data;
using ClientReportManager.Models;
using ClientReportManager.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Se habilita MVC para trabajar con controladores y vistas Razor.
builder.Services.AddControllersWithViews();
// Se registra el DbContext usando SQL Server.
// La cadena de conexión se toma desde appsettings.json.
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// Se configura autenticación por cookies para proteger las pantallas internas del sistema.
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/Login";
options.ExpireTimeSpan = TimeSpan.FromHours(8);
options.SlidingExpiration = true;
});
// Se registra PasswordHasher para validar contraseñas de forma segura.
builder.Services.AddScoped<IPasswordHasher<Usuario>, PasswordHasher<Usuario>>();
// Se registra el servicio de clientes para separar la lógica del controlador.
builder.Services.AddScoped<IClienteService, ClienteService>();
// Se registra el servicio del dashboard para centralizar las consultas de resumen.
builder.Services.AddScoped<IDashboardService, DashboardService>();
// Se registra el servicio de reportes para centralizar filtros, resúmenes y consultas administrativas.
builder.Services.AddScoped<IReporteService, ReporteService>();
// Servicio de usuarios utilizado por el proceso de login.
builder.Services.AddScoped<IUsuarioService, UsuarioService>();
var app = builder.Build();
// Configuración de errores para ambientes diferentes a desarrollo.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// Middleware base de la aplicación web.
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// La autenticación debe ejecutarse antes de la autorización.
app.UseAuthentication();
app.UseAuthorization();
// Ruta principal del sistema.
// En esta etapa, el Dashboard estará protegido y solicitará login si el usuario no está autenticado.
app.MapControllerRoute(
name: "default",
pattern: "{controller=Dashboard}/{action=Index}/{id?}");
app.Run();