Arreglado fallo de aunteticación y agregado endpoint de subir archivo (sin probar)
This commit is contained in:
@@ -7,6 +7,9 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.2.0" />
|
||||||
|
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.2.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
58
APIFicheros/Controllers/AuthController.cs
Normal file
58
APIFicheros/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using APIFicheros.DTOs;
|
||||||
|
using bdAsegasa;
|
||||||
|
using bdAsegasa.db;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using tsUtilidades;
|
||||||
|
|
||||||
|
namespace APIFicheros.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class AuthController : Controller
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
public AuthController(IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
[AllowAnonymous]
|
||||||
|
[HttpPost("login")]
|
||||||
|
public IActionResult Login([FromBody] DatosAuth loginDto)
|
||||||
|
{
|
||||||
|
|
||||||
|
string token = "";
|
||||||
|
string datosLoginUser = _configuration["DatosLogin:Usuario"];
|
||||||
|
string datosLoginPassword = _configuration["DatosLogin:Password"];
|
||||||
|
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
return BadRequest(ModelState);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loginDto.usuario == datosLoginUser && loginDto.password == datosLoginPassword)
|
||||||
|
{
|
||||||
|
token = Utilidades.AuthenticateUser(loginDto, _configuration);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Unauthorized("Nombre de usuario o contraseña incorrectos.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
Token = token
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
using APIFicheros.DTOs;
|
using APIFicheros.DTOs;
|
||||||
using bdAsegasa;
|
using bdAsegasa;
|
||||||
using bdAsegasa.db;
|
using bdAsegasa.db;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
@@ -10,11 +12,11 @@ namespace APIFicheros.Controllers
|
|||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class ObtenerFicherosController : Controller
|
public class ManipularFicherosController : Controller
|
||||||
{
|
{
|
||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private bdAsegasa.tscgestionasegasa bd;
|
private bdAsegasa.tscgestionasegasa bd;
|
||||||
public ObtenerFicherosController(IConfiguration configuration)
|
public ManipularFicherosController(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
|
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
@@ -25,14 +27,12 @@ namespace APIFicheros.Controllers
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpPost("ObtenerFicherosFaltantes")]
|
[HttpPost("ObtenerFicherosFaltantes")]
|
||||||
public ActionResult PostNombreBD([FromBody] string numeroPoliza)
|
[Authorize]
|
||||||
|
public ActionResult PostComprobarFicheros([FromBody] string numeroPoliza)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
int idPoliza = bd.polizassg.First(x => x.NumeroPoliza!.Contains(numeroPoliza)).idPoliza;
|
int idPoliza = bd.polizassg.First(x => x.NumeroPoliza!.Contains(numeroPoliza)).idPoliza;
|
||||||
|
|
||||||
|
|
||||||
@@ -54,7 +54,8 @@ namespace APIFicheros.Controllers
|
|||||||
return Ok(listadoFicherosFaltantes);
|
return Ok(listadoFicherosFaltantes);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
|
||||||
return BadRequest("Ha ocurrido un error. Mensaje de error: " + ex.Message);
|
return BadRequest("Ha ocurrido un error. Mensaje de error: " + ex.Message);
|
||||||
}
|
}
|
||||||
@@ -63,5 +64,47 @@ namespace APIFicheros.Controllers
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpPost("SubirFicherosFaltantes")]
|
||||||
|
[Authorize]
|
||||||
|
public ActionResult PostSubirFicheros([FromBody] DatosFicheros datosFichero)
|
||||||
|
{
|
||||||
|
|
||||||
|
var transaction = bd.Database.BeginTransaction();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
byte[] contenidoFichero;
|
||||||
|
var ms = new MemoryStream();
|
||||||
|
datosFichero.fichero.CopyTo(ms);
|
||||||
|
contenidoFichero = ms.ToArray();
|
||||||
|
|
||||||
|
int idFichero = Utilidades.guardarFichero(bd, contenidoFichero, datosFichero.fichero.FileName, datosFichero.descripcion);
|
||||||
|
|
||||||
|
|
||||||
|
documentospolizassg documentoObtenido = bd.documentospolizassg.First( x => x.idDocumento == datosFichero.idDocumento);
|
||||||
|
|
||||||
|
documentoObtenido.idFichero = idFichero;
|
||||||
|
|
||||||
|
bd.Update(documentoObtenido);
|
||||||
|
|
||||||
|
bd.SaveChanges();
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
return Ok();
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
return BadRequest("Ha ocurrido un error. Mensaje de error: " + ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace APIFicheros.Controllers;
|
|
||||||
|
|
||||||
[ApiController]
|
|
||||||
[Route("[controller]")]
|
|
||||||
public class WeatherForecastController : ControllerBase
|
|
||||||
{
|
|
||||||
private static readonly string[] Summaries = new[]
|
|
||||||
{
|
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly ILogger<WeatherForecastController> _logger;
|
|
||||||
|
|
||||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet(Name = "GetWeatherForecast")]
|
|
||||||
public IEnumerable<WeatherForecast> Get()
|
|
||||||
{
|
|
||||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
|
||||||
{
|
|
||||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
||||||
TemperatureC = Random.Shared.Next(-20, 55),
|
|
||||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
|
||||||
})
|
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
8
APIFicheros/DTOs/DatosAuth.cs
Normal file
8
APIFicheros/DTOs/DatosAuth.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
namespace APIFicheros.DTOs
|
||||||
|
{
|
||||||
|
public class DatosAuth
|
||||||
|
{
|
||||||
|
public string usuario { get; set; }
|
||||||
|
public string password { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
9
APIFicheros/DTOs/DatosFicheros.cs
Normal file
9
APIFicheros/DTOs/DatosFicheros.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
namespace APIFicheros.DTOs
|
||||||
|
{
|
||||||
|
public class DatosFicheros
|
||||||
|
{
|
||||||
|
public int idDocumento { get; set; }
|
||||||
|
public IFormFile fichero { get; set; }
|
||||||
|
public string descripcion { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,32 +1,130 @@
|
|||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.OpenApi.Models;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// 1. Configuraci<63>n de Servicios
|
||||||
|
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
|
||||||
builder.Services.AddSwaggerGen();
|
|
||||||
|
|
||||||
|
|
||||||
|
// a. Configurar servicios de controladores
|
||||||
builder.Services.AddControllers()
|
builder.Services.AddControllers()
|
||||||
.AddJsonOptions(options =>
|
.ConfigureApiBehaviorOptions(options =>
|
||||||
|
{
|
||||||
|
options.SuppressModelStateInvalidFilter = true; // Desactiva la validaci<63>n autom<6F>tica del estado del modelo
|
||||||
|
})
|
||||||
|
.AddJsonOptions(options =>
|
||||||
|
{
|
||||||
|
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
|
||||||
|
});
|
||||||
|
|
||||||
|
// b. Configuraci<63>n de JWT
|
||||||
|
var jwtSettings = builder.Configuration.GetSection("Jwt");
|
||||||
|
var keyString = jwtSettings["Key"];
|
||||||
|
if (string.IsNullOrEmpty(keyString))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException("JWT Key is not configured.");
|
||||||
|
}
|
||||||
|
var key = Encoding.UTF8.GetBytes(keyString);
|
||||||
|
|
||||||
|
builder.Services.AddAuthentication(options =>
|
||||||
|
{
|
||||||
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
|
})
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
options.JsonSerializerOptions.ReferenceHandler =
|
ValidateIssuer = true,
|
||||||
System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
|
ValidIssuer = jwtSettings["Issuer"],
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidAudience = jwtSettings["Audience"],
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["Key"])),
|
||||||
|
ValidAlgorithms = new[] { SecurityAlgorithms.HmacSha256 }
|
||||||
|
};
|
||||||
|
|
||||||
|
options.Events = new JwtBearerEvents
|
||||||
|
{
|
||||||
|
OnAuthenticationFailed = ctx =>
|
||||||
|
{
|
||||||
|
var logger = ctx.HttpContext.RequestServices.GetRequiredService<ILogger<Program>>();
|
||||||
|
logger.LogError("NOMBRE FALLO {0}", ctx.Exception.GetType().Name);
|
||||||
|
logger.LogError("MENSAJE FALLO {0}", ctx.Exception.Message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// d. Configurar Swagger con soporte para JWT
|
||||||
|
builder.Services.AddSwaggerGen(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "SwaggerCamcue API", Version = "v1" });
|
||||||
|
|
||||||
|
// Definir el esquema de seguridad JWT
|
||||||
|
var securityScheme = new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Name = "Authorization",
|
||||||
|
Description = "Ingrese 'Bearer' seguido de su token en el campo de texto.\n\nEjemplo: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6...'",
|
||||||
|
In = ParameterLocation.Header,
|
||||||
|
Type = SecuritySchemeType.Http,
|
||||||
|
Scheme = "Bearer",
|
||||||
|
BearerFormat = "JWT"
|
||||||
|
};
|
||||||
|
|
||||||
|
c.AddSecurityDefinition("Bearer", securityScheme);
|
||||||
|
|
||||||
|
|
||||||
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference = new OpenApiReference
|
||||||
|
{
|
||||||
|
Type = ReferenceType.SecurityScheme,
|
||||||
|
Id = "Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Array.Empty<string>()
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// 2. Configuraci<EFBFBD>n del Pipeline HTTP
|
||||||
|
|
||||||
|
// b. Habilitar Swagger solo en Desarrollo
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI();
|
app.UseSwaggerUI(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "SwaggerCamcue API V1");
|
||||||
|
c.RoutePrefix = string.Empty; // Swagger en la ra<72>z
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseCors(policy =>
|
||||||
|
{
|
||||||
|
policy.AllowAnyOrigin()
|
||||||
|
.AllowAnyMethod()
|
||||||
|
.AllowAnyHeader();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//app.UseHttpsRedirection();
|
||||||
|
// d. Autenticaci<63>n y Autorizaci<63>n
|
||||||
|
app.UseAuthentication();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
// e. Mapear Controladores
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|
||||||
|
// f. Ejecutar la Aplicaci<63>n
|
||||||
app.Run();
|
app.Run();
|
||||||
|
|||||||
90
APIFicheros/Utilidades.cs
Normal file
90
APIFicheros/Utilidades.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using APIFicheros.DTOs;
|
||||||
|
using bdAsegasa;
|
||||||
|
using bdAsegasa.db;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
using tsUtilidades;
|
||||||
|
namespace APIFicheros
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public class Utilidades
|
||||||
|
{
|
||||||
|
public static string AuthenticateUser(DatosAuth loginDto, IConfiguration _configuration)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
// Generar token JWT
|
||||||
|
var jwtSettings = _configuration.GetSection("Jwt");
|
||||||
|
var keyBytes = Encoding.UTF8.GetBytes(jwtSettings["Key"]);
|
||||||
|
var key = new SymmetricSecurityKey(keyBytes);
|
||||||
|
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||||
|
|
||||||
|
|
||||||
|
var claims = new List<Claim>
|
||||||
|
{
|
||||||
|
new Claim("usu", "1"),
|
||||||
|
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var expiration = DateTime.UtcNow.AddMinutes(int.Parse(jwtSettings["ExpireMinutes"]));
|
||||||
|
|
||||||
|
var token = new JwtSecurityToken(
|
||||||
|
issuer: jwtSettings["Issuer"],
|
||||||
|
audience: jwtSettings["Audience"],
|
||||||
|
claims: claims,
|
||||||
|
expires: expiration,
|
||||||
|
signingCredentials: creds
|
||||||
|
);
|
||||||
|
|
||||||
|
var tokenString = new JwtSecurityTokenHandler().WriteToken(token);
|
||||||
|
|
||||||
|
return tokenString;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new Exception($"Error en la autenticación: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int guardarFichero(bdAsegasa.tscgestionasegasa bd, byte[] fichero,string nombreFichero ,string? descripcion)
|
||||||
|
{
|
||||||
|
int idFichero = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
ficheros nuevoFichero = new ficheros();
|
||||||
|
|
||||||
|
|
||||||
|
nuevoFichero.Fichero = fichero;
|
||||||
|
nuevoFichero.NombreFichero = nombreFichero;
|
||||||
|
nuevoFichero.Descripcion = descripcion;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bd.ficheros.Add(nuevoFichero);
|
||||||
|
|
||||||
|
bd.SaveChanges();
|
||||||
|
|
||||||
|
|
||||||
|
idFichero = nuevoFichero.idFichero;
|
||||||
|
return idFichero;
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
|
||||||
|
throw new Exception("Excepción: " + ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace APIFicheros;
|
|
||||||
|
|
||||||
public class WeatherForecast
|
|
||||||
{
|
|
||||||
public DateOnly Date { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureC { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
|
|
||||||
public string? Summary { get; set; }
|
|
||||||
}
|
|
||||||
@@ -1,11 +1,20 @@
|
|||||||
{
|
{
|
||||||
|
"Jwt": {
|
||||||
|
"Key": "Asegasa_APIFicheros2025_5849651882",
|
||||||
|
"Issuer": "MiAPI_AuthServer",
|
||||||
|
"Audience": "TuAudience",
|
||||||
|
"ExpireMinutes":1
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"DatosLogin": {
|
||||||
|
"Usuario": "ASEGASA",
|
||||||
|
"Password": "Asegasa.2025"
|
||||||
|
},
|
||||||
"Configuracion": {
|
"Configuracion": {
|
||||||
"SegundosMinimosEntreProcesos": "60",
|
"SegundosMinimosEntreProcesos": "60",
|
||||||
"HoraProcesosDiarios": "06:30",
|
"HoraProcesosDiarios": "06:30",
|
||||||
|
|||||||
Reference in New Issue
Block a user