Arreglado fallo de aunteticación y agregado endpoint de subir archivo (sin probar)
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user