using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System.Text; using GestionIntegral.Api.Data; using GestionIntegral.Api.Services.Contables; using GestionIntegral.Api.Services.Distribucion; using GestionIntegral.Api.Data.Repositories.Contables; using GestionIntegral.Api.Data.Repositories.Distribucion; using GestionIntegral.Api.Data.Repositories.Impresion; using GestionIntegral.Api.Services.Impresion; using GestionIntegral.Api.Services.Usuarios; using GestionIntegral.Api.Data.Repositories.Usuarios; var builder = WebApplication.CreateBuilder(args); // --- Registros de Servicios --- builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); // --- Configuración de Autenticación JWT --- var jwtSettings = builder.Configuration.GetSection("Jwt"); var jwtKey = jwtSettings["Key"] ?? throw new ArgumentNullException("Jwt:Key", "JWT Key not configured in appsettings"); var keyBytes = Encoding.ASCII.GetBytes(jwtKey); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = builder.Environment.IsProduction(); options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(keyBytes), ValidateIssuer = true, ValidIssuer = jwtSettings["Issuer"], ValidateAudience = true, ValidAudience = jwtSettings["Audience"], ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; }); // --- Configuración de Autorización --- builder.Services.AddAuthorization(); // --- Configuración de CORS --- // <--- MOVER AQUÍ LA CONFIGURACIÓN DE SERVICIOS CORS var MyAllowSpecificOrigins = "_myAllowSpecificOrigins"; builder.Services.AddCors(options => { options.AddPolicy(name: MyAllowSpecificOrigins, policy => { policy.WithOrigins("http://localhost:5173") // URL Frontend React .AllowAnyHeader() .AllowAnyMethod(); // Para pruebas más permisivas (NO USAR EN PRODUCCIÓN): // policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); }); }); // --- Fin CORS --- // --- Servicios del Contenedor --- builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // --- Configuración del Pipeline HTTP --- if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } // ¡¡¡NO USAR UseHttpsRedirection si tu API corre en HTTP!!! // Comenta o elimina la siguiente línea si SÓLO usas http://localhost:5183 // app.UseHttpsRedirection(); // <--- COMENTAR/ELIMINAR SI NO USAS HTTPS EN API // --- Aplicar CORS ANTES de Autenticación/Autorización --- app.UseCors(MyAllowSpecificOrigins); // --- Fin aplicar CORS --- app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();