129 lines
4.9 KiB
C#
129 lines
4.9 KiB
C#
using bdCOAS.db;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SwaggerAutenticacion.Clases;
|
|
using System.Linq.Dynamic.Core;
|
|
using tsUtilidades;
|
|
using System.Security.Claims;
|
|
|
|
|
|
namespace SwaggerAutenticacion.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class AuthController : Controller
|
|
{
|
|
|
|
private readonly IConfiguration jwtSettings;
|
|
|
|
public AuthController(IConfiguration configuration)
|
|
{
|
|
|
|
jwtSettings = configuration.GetSection("Jwt");
|
|
}
|
|
|
|
[HttpPost("ComprobarUser")]
|
|
public ActionResult<string> PostComprobarUser([FromBody] DatosAuth model)
|
|
{
|
|
var bd = bdCOAS.tsCOAS.NuevoContextoDirecto();
|
|
|
|
|
|
|
|
|
|
try
|
|
{
|
|
autorizacionesexternas externo = bd.autorizacionesexternas.FirstOrDefault(x => x.Codigo == model.nombreExterno);
|
|
|
|
//var externo = bd.autorizacionesexternas.FirstOrDefault(x => x.Nombre == model.nombreExterno);
|
|
|
|
if (externo != null)
|
|
{
|
|
var pwEmpresaEncrypt = tsUtilidades.crypt.SHA1("M3Soft." + model.pwExterno);
|
|
|
|
if (pwEmpresaEncrypt != externo.HashPassword) throw new Exception("PassWord Incorrecta");
|
|
}
|
|
else
|
|
{
|
|
throw new Exception("Autorización no encontrada.");
|
|
}
|
|
|
|
|
|
var token = "";
|
|
try
|
|
{
|
|
var sContraseñaenc = tsUtilidades.crypt.SHA1("M3Soft." + model.pwColegiado);
|
|
Exception ex = null;
|
|
accesoswebcoas nac = null;
|
|
var col = bd.Colegiados.First(x => x.NumeroColegiado == model.identificacionColegiado || x.NIF == model.identificacionColegiado);
|
|
clavesacceso.LoginCoas(bd, col.NumeroColegiado, sContraseñaenc, "", clavesacceso.TipoLoginEnum.EXTERNOS_1, ref ex, ref nac);
|
|
if (ex != null) throw ex;
|
|
|
|
externo.FechaUltimaConexion = DateTime.Now;
|
|
bd.Update(externo);
|
|
bd.SaveChanges();
|
|
|
|
token = AuthHandler.GenerateJwtToken(jwtSettings, col.idColegiado.ToString());
|
|
return Ok(new { token });
|
|
}
|
|
catch (tsExcepcion e)
|
|
{
|
|
return Unauthorized("Credenciales Colegiado no válidas. " + e.Message);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return BadRequest($"Ha ocurrido un error. Mensaje de error: {e.Message}");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return Unauthorized("Credenciales Empresa no válidas. " + e.Message);
|
|
}
|
|
}
|
|
|
|
|
|
[HttpPost("LoginExterno")]
|
|
public ActionResult<object> PostLoginExterno([FromBody] DatosAuthEmail model)
|
|
{
|
|
var bd = bdCOAS.tsCOAS.NuevoContextoDirecto();
|
|
// string ip = HttpContext.Connection.RemoteIpAddress?.ToString();
|
|
try
|
|
{
|
|
var externo = bd.autorizacionesexternas
|
|
.FirstOrDefault(x => x.Codigo == model.nombreExterno)
|
|
?? throw new Exception("Autorización no encontrada.");
|
|
|
|
var pwEncrypt = tsUtilidades.crypt.SHA1("M3Soft." + model.pwExterno);
|
|
if (pwEncrypt != externo.HashPassword)
|
|
throw new Exception("Password Incorrecta.");
|
|
|
|
// Solo guardamos nombreExterno + tipo en el token
|
|
var claims = new List<Claim>
|
|
{
|
|
new Claim(ClaimTypes.Name, model.nombreExterno),
|
|
new Claim("AuthType", "EmailOnly")
|
|
};
|
|
|
|
var token = AuthHandler.GenerateJwtToken(jwtSettings, claims);
|
|
|
|
externo.FechaUltimaConexion = DateTime.Now;
|
|
bd.Update(externo);
|
|
bd.SaveChanges();
|
|
// if (externo.Codigo!="GCOAS") tsUtilidades.TsNotificacionesClient.RegistrarAsync("LoginExterno", "Login Externo " + externo.Nombre + " - IP: " + ip, TsNotificacionesClient.TipoNotificacionEnum.INFO);
|
|
return Ok(new { token });
|
|
}
|
|
catch (Exception e) when (
|
|
e.Message.Contains("no encontrada") ||
|
|
e.Message.Contains("Incorrecta"))
|
|
{
|
|
tsUtilidades.TsNotificacionesClient.RegistrarAsync("Credenciales LoginExterno incorrectas", e.Message,TsNotificacionesClient.TipoNotificacionEnum.CRÍTICO);
|
|
return Unauthorized("Credenciales Empresa no válidas. " + e.Message);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
tsUtilidades.TsNotificacionesClient.RegistrarAsync("Error en LoginExterno", e.Message, TsNotificacionesClient.TipoNotificacionEnum.ERROR);
|
|
return BadRequest($"Ha ocurrido un error: {e.Message}");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|