Arreglado fallo de aunteticación y agregado endpoint de subir archivo (sin probar)
This commit is contained in:
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 bdAsegasa;
|
||||
using bdAsegasa.db;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
@@ -10,11 +12,11 @@ namespace APIFicheros.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ObtenerFicherosController : Controller
|
||||
public class ManipularFicherosController : Controller
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private bdAsegasa.tscgestionasegasa bd;
|
||||
public ObtenerFicherosController(IConfiguration configuration)
|
||||
public ManipularFicherosController(IConfiguration configuration)
|
||||
{
|
||||
|
||||
_configuration = configuration;
|
||||
@@ -25,14 +27,12 @@ namespace APIFicheros.Controllers
|
||||
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("ObtenerFicherosFaltantes")]
|
||||
public ActionResult PostNombreBD([FromBody] string numeroPoliza)
|
||||
[Authorize]
|
||||
public ActionResult PostComprobarFicheros([FromBody] string numeroPoliza)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
int idPoliza = bd.polizassg.First(x => x.NumeroPoliza!.Contains(numeroPoliza)).idPoliza;
|
||||
|
||||
|
||||
@@ -54,8 +54,9 @@ namespace APIFicheros.Controllers
|
||||
return Ok(listadoFicherosFaltantes);
|
||||
|
||||
}
|
||||
catch (Exception ex) {
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user