88 lines
3.0 KiB
C#
88 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
using GestionIntegral.Api.Data;
|
|
using GestionIntegral.Api.Services;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// --- Registros de Servicios ---
|
|
builder.Services.AddSingleton<DbConnectionFactory>();
|
|
builder.Services.AddScoped<PasswordHasherService>();
|
|
builder.Services.AddScoped<IAuthRepository, AuthRepository>(); // Asegúrate que el namespace sea correcto
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
|
|
// --- 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(); |