2025-10-13 10:40:20 -03:00
|
|
|
// backend/Program.cs
|
2025-10-02 14:48:37 -03:00
|
|
|
using Inventario.API.Data;
|
2025-10-13 10:40:20 -03:00
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.OpenApi.Models;
|
2025-10-02 14:48:37 -03:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2025-10-13 10:40:20 -03:00
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
|
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!))
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-02 14:48:37 -03:00
|
|
|
// Add services to the container.
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2025-10-04 22:17:05 -03:00
|
|
|
|
2025-10-13 10:40:20 -03:00
|
|
|
// CONFIGURACIÓN DE SWAGGER
|
|
|
|
|
builder.Services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
// 1. Definir el esquema de seguridad (JWT Bearer)
|
|
|
|
|
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Description = "Autenticación JWT usando el esquema Bearer. " +
|
|
|
|
|
"Introduce 'Bearer' [espacio] y luego tu token en el campo de abajo. " +
|
|
|
|
|
"Ejemplo: 'Bearer 12345abcdef'",
|
|
|
|
|
Name = "Authorization", // El nombre del header
|
|
|
|
|
In = ParameterLocation.Header, // Dónde se envía (en la cabecera)
|
|
|
|
|
Type = SecuritySchemeType.ApiKey, // Tipo de esquema
|
|
|
|
|
Scheme = "Bearer"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 2. Aplicar el requisito de seguridad globalmente a todos los endpoints
|
|
|
|
|
options.AddSecurityRequirement(new OpenApiSecurityRequirement()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Reference = new OpenApiReference
|
|
|
|
|
{
|
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer" // Debe coincidir con el Id de AddSecurityDefinition
|
|
|
|
|
},
|
|
|
|
|
Scheme = "oauth2",
|
|
|
|
|
Name = "Bearer",
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
},
|
|
|
|
|
new List<string>()
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// --- DEFINIR LA POLÍTICA CORS ---
|
2025-10-04 22:17:05 -03:00
|
|
|
// Definimos un nombre para nuestra política
|
|
|
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
|
|
|
|
|
|
|
|
// Añadimos el servicio de CORS y configuramos la política
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy(name: MyAllowSpecificOrigins,
|
|
|
|
|
policy =>
|
|
|
|
|
{
|
|
|
|
|
// Permitimos explícitamente el origen de tu frontend (Vite)
|
|
|
|
|
policy.WithOrigins("http://localhost:5173")
|
|
|
|
|
.AllowAnyHeader() // Permitir cualquier encabezado
|
|
|
|
|
.AllowAnyMethod(); // Permitir GET, POST, PUT, DELETE, etc.
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
// -----------------------------------
|
|
|
|
|
|
2025-10-02 14:48:37 -03:00
|
|
|
builder.Services.AddSingleton<DapperContext>();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
2025-10-02 15:08:49 -03:00
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(c =>
|
|
|
|
|
{
|
|
|
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Inventario API V1");
|
|
|
|
|
c.RoutePrefix = string.Empty;
|
|
|
|
|
});
|
2025-10-02 14:48:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
2025-10-04 22:17:05 -03:00
|
|
|
|
2025-10-13 10:40:20 -03:00
|
|
|
// --- ACTIVAR EL MIDDLEWARE DE CORS ---
|
2025-10-04 22:17:05 -03:00
|
|
|
// ¡IMPORTANTE! Debe ir ANTES de MapControllers y DESPUÉS de UseHttpsRedirection (si se usa)
|
|
|
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
2025-10-13 10:40:20 -03:00
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2025-10-02 15:08:49 -03:00
|
|
|
app.MapControllers();
|
2025-10-04 22:17:05 -03:00
|
|
|
|
2025-10-02 15:08:49 -03:00
|
|
|
app.Run();
|