primera version estable
This commit is contained in:
128
swEmtusa/Controllers/AuthController.cs
Normal file
128
swEmtusa/Controllers/AuthController.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
27
swEmtusa/Program.cs
Normal file
27
swEmtusa/Program.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using bdEmtusa;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// var bd = tscEmtusa.NuevoContexto();
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
41
swEmtusa/Properties/launchSettings.json
Normal file
41
swEmtusa/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:12279",
|
||||
"sslPort": 44348
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5245",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7110;http://localhost:5245",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
swEmtusa/appsettings.Development.json
Normal file
8
swEmtusa/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
swEmtusa/appsettings.json
Normal file
9
swEmtusa/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
25
swEmtusa/swEmtusa.csproj
Normal file
25
swEmtusa/swEmtusa.csproj
Normal file
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="Controllers\AuthController.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\bdEmtusa\bdEmtusa.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Controllers\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
6
swEmtusa/swEmtusa.http
Normal file
6
swEmtusa/swEmtusa.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@swEmtusa_HostAddress = http://localhost:5245
|
||||
|
||||
GET {{swEmtusa_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
Reference in New Issue
Block a user