Skip to content

Commit

Permalink
update references nudget
Browse files Browse the repository at this point in the history
update references  nudget
  • Loading branch information
PabloDiazSSA committed Apr 9, 2024
1 parent afd3b58 commit 7ee2416
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 deletions.
8 changes: 7 additions & 1 deletion Controllers/AuthenticationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ namespace Entregable2_PD.Api.Controllers
public class AuthenticationController : ControllerBase
{
private readonly IConfiguration _config;
private readonly ILogger<AuthenticationController> _logger;

/// <summary>
///
/// </summary>
/// <param name="config"></param>
public AuthenticationController(IConfiguration config)
/// <param name="logger"></param>
public AuthenticationController(IConfiguration config, ILogger<AuthenticationController> logger)
{
_config = config;
_logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -81,6 +85,7 @@ public ClsResponse<string> Login(UserDto userDto)
#if DEBUG
Console.WriteLine(ex.Message);
#endif
_logger.LogWarning(msg);
cls.ErrorMessage = msg;
return cls;
throw;
Expand Down Expand Up @@ -127,6 +132,7 @@ private string GenerateToken(UserModel administrator)
#if DEBUG
Console.WriteLine(ex.Message);
#endif
_logger.LogError(ex.Message, ex);
return string.Empty;
throw;
}
Expand Down
6 changes: 5 additions & 1 deletion Controllers/CardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Entregable2_PD.Api.Controllers
public class CardController : ControllerBase
{
private readonly IConfiguration _config;
private readonly ILogger<CardController> _logger;
/// <summary>
/// Almacena el numero de tarjeta original
/// </summary>
Expand All @@ -44,9 +45,11 @@ public class CardController : ControllerBase
/// Ctor
/// </summary>
/// <param name="config"></param>
public CardController(IConfiguration config)
/// <param name="logger"></param>
public CardController(IConfiguration config, ILogger<CardController> logger)
{
_config = config;
_logger = logger;
A = string.Empty;
B = string.Empty;
BC = string.Empty;
Expand Down Expand Up @@ -143,6 +146,7 @@ public ClsResponse<dynamic> SetCard(CardDto card)
#if DEBUG
Console.WriteLine(ex.Message);
#endif
_logger.LogWarning(msg);
response.ErrorMessage = msg;
return response;
}
Expand Down
49 changes: 31 additions & 18 deletions Controllers/FormController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ public class FormController : ControllerBase
///
/// </summary>
private readonly IConfiguration _config;
private readonly ILogger<FormController> _logger;
/// <summary>
/// Ctor
/// </summary>
/// <param name="config"></param>
public FormController(IConfiguration config)
/// <param name="logger"></param>
public FormController(IConfiguration config, ILogger<FormController> logger)
{
_config = config;
_logger = logger;
}
/// <summary>
/// Metodo de ejemplos de seguridad usando regex y datannotations en campos de un formulario
Expand All @@ -35,28 +38,38 @@ public FormController(IConfiguration config)
[HttpPost("Set")]
public ClsResponse<dynamic> SetForm(FormDto form)
{
if (HttpContext.User.Identity is not ClaimsIdentity identity)
try
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthenticated" };
}
foreach (var claim in identity.Claims)
{
Console.WriteLine($"Claim Type: {claim.Type}, Claim Value: {claim.Value}");
}
var rToken = Jwt.ValidateToken(identity, _config);
var user = rToken;
if (HttpContext.User.Identity is not ClaimsIdentity identity)
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthenticated" };
}
foreach (var claim in identity.Claims)
{
Console.WriteLine($"Claim Type: {claim.Type}, Claim Value: {claim.Value}");
}
var rToken = Jwt.ValidateToken(identity, _config);
var user = rToken;

if (user is null || user.Role is null)
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthenticated" };
}
if (user is null || user.Role is null)
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthenticated" };
}

if (user.Role.ToUpper() != "ADMIN")
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthorized" };
if (user.Role.ToUpper() != "ADMIN")
{
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Unauthorized" };
}

return new ClsResponse<dynamic> { Error = true, Data = form };
}
catch (Exception ex)
{
_logger.LogWarning(ex.Message, ex);
return new ClsResponse<dynamic> { Error = true, ErrorMessage = "Ocurrio un error, reintentarlo más tarde" };

return new ClsResponse<dynamic> { Error = true, Data = form };
}

}


Expand Down
5 changes: 3 additions & 2 deletions Entregable2-PD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Entregable2_PD</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<DocumentationFile>C:\Users\pdiaz\source\GitHub\ProyectoFinal-PD\ProyectoFinal.Api\bin\Debug\net6.0\Entregable2-PD.xml</DocumentationFile>
<DocumentationFile>C:\Users\pdiaz\source\GitHub\Entregable2-PD\bin\Debug\net6.0\nlog-2023-12-05.log</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -19,8 +19,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.3.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions NLog.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="File" name="file" fileName="nlog-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
6 changes: 6 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using NLog.Web;
using System.Reflection;
using System.Text;

var builder = WebApplication.CreateBuilder(args);


builder.Logging.ClearProviders();
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Host.UseNLog();

// Add services to the container.

builder.Services.AddControllers();
Expand Down
7 changes: 7 additions & 0 deletions appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"NLog": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"AllowedHosts": "*",
Expand Down
9 changes: 8 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"NLog": {
"LogLevel": {
"Default": "Trace",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"AllowedHosts": "*"
}
}

0 comments on commit 7ee2416

Please sign in to comment.