JwtMiddleware berakva
This commit is contained in:
@@ -41,7 +41,7 @@ namespace WorkFlowCheck.API.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("GetCheckPoints/{id}")]
|
[HttpGet("GetCheckPoints/{id}")]
|
||||||
public async Task<ApiResponseDTO<CheckPointDTO>> GetAllCheckpointsAsync(int id)
|
public async Task<ApiResponseDTO<CheckPointDTO>> GetCheckpointsAsync(int id)
|
||||||
{
|
{
|
||||||
var retVal = new ApiResponseDTO<CheckPointDTO>()
|
var retVal = new ApiResponseDTO<CheckPointDTO>()
|
||||||
{
|
{
|
||||||
@@ -136,7 +136,7 @@ namespace WorkFlowCheck.API.Controllers
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("authenticate")]
|
[HttpPost("UpdateCheckPoint")]
|
||||||
public async Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint([FromBody] CheckPointDTO checkPointDTO)
|
public async Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint([FromBody] CheckPointDTO checkPointDTO)
|
||||||
{
|
{
|
||||||
var retVal = new ApiResponseDTO<CheckPointDTO>()
|
var retVal = new ApiResponseDTO<CheckPointDTO>()
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
namespace WorkFlowCheck.API
|
namespace WorkFlowCheck.API.Middleware
|
||||||
{
|
{
|
||||||
public class ApiKeyMiddleware
|
public class ApiKeyMiddleware
|
||||||
{
|
{
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace WorkFlowCheck.API.Middleware
|
||||||
|
{
|
||||||
|
public class JwtMiddleware
|
||||||
|
{
|
||||||
|
private readonly RequestDelegate _next;
|
||||||
|
public readonly IConfiguration _configuration;
|
||||||
|
public JwtMiddleware(RequestDelegate next, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_next = next;
|
||||||
|
_configuration = configuration;
|
||||||
|
}
|
||||||
|
public async Task Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
|
||||||
|
if (!string.IsNullOrEmpty(token) && ValidateToken(token, out var claims))
|
||||||
|
{
|
||||||
|
context.User = new ClaimsPrincipal(new ClaimsIdentity(claims, "jwt"));
|
||||||
|
}
|
||||||
|
await _next(context);
|
||||||
|
}
|
||||||
|
private bool ValidateToken(string token, out Claim[] claims)
|
||||||
|
{
|
||||||
|
var jwtSettings = _configuration.GetSection("Jwt");
|
||||||
|
var secretKey = jwtSettings["SecretKey"];
|
||||||
|
|
||||||
|
claims = null;
|
||||||
|
var tokenHandler = new JwtSecurityTokenHandler();
|
||||||
|
var key = Encoding.UTF8.GetBytes(secretKey);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var principal = tokenHandler.ValidateToken(token, new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||||
|
ValidateIssuer = false,
|
||||||
|
ValidateAudience = false,
|
||||||
|
ClockSkew = TimeSpan.Zero
|
||||||
|
}, out var validatedToken);
|
||||||
|
|
||||||
|
claims = principal.Claims.ToArray();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using WorkFlowCheck.API;
|
|
||||||
using WorkFlowCheck.DL;
|
using WorkFlowCheck.DL;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
@@ -11,6 +10,7 @@ using WorkFlowCheck.BL.Infra;
|
|||||||
using WorkFlowCheck.DL.Seed;
|
using WorkFlowCheck.DL.Seed;
|
||||||
using WorkFlowCheck.Common.RequestContext;
|
using WorkFlowCheck.Common.RequestContext;
|
||||||
using WorkFlowCheck.API.RequestContext;
|
using WorkFlowCheck.API.RequestContext;
|
||||||
|
using WorkFlowCheck.API.Middleware;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@@ -85,6 +85,8 @@ builder.Services.AddCors(options =>
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
var context = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||||
@@ -104,7 +106,10 @@ if (app.Environment.IsDevelopment())
|
|||||||
// Routing és endpointok
|
// Routing és endpointok
|
||||||
//app.UseHttpsRedirection();
|
//app.UseHttpsRedirection();
|
||||||
app.UseRouting();
|
app.UseRouting();
|
||||||
|
|
||||||
app.UseMiddleware<ApiKeyMiddleware>();
|
app.UseMiddleware<ApiKeyMiddleware>();
|
||||||
|
app.UseMiddleware<JwtMiddleware>();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
app.UseCors();
|
app.UseCors();
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ namespace WorkFlowCheck.BL.Mappings
|
|||||||
.ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));
|
.ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));
|
||||||
CreateMap<CheckListTemplateRow, CheckListTemplateRowDTO>();
|
CreateMap<CheckListTemplateRow, CheckListTemplateRowDTO>();
|
||||||
CreateMap<CheckPoint, CheckPointDTO>().ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));
|
CreateMap<CheckPoint, CheckPointDTO>().ForMember(dest => dest.CheckListTemplateRowDTO, opt => opt.MapFrom(src => src.CheckListTemplateRows));
|
||||||
|
CreateMap<CheckPointDTO, CheckPoint>();
|
||||||
CreateMap<Location, LocationDTO>();
|
CreateMap<Location, LocationDTO>();
|
||||||
CreateMap<Equipment, EquipmentDTO>();
|
CreateMap<Equipment, EquipmentDTO>();
|
||||||
|
|
||||||
|
|||||||
@@ -16,17 +16,14 @@ namespace WorkFlowCheck.DL
|
|||||||
{
|
{
|
||||||
public class AppDbContext : DbContext
|
public class AppDbContext : DbContext
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
#if !DEBUG
|
|
||||||
private readonly IRequestContext _requestContext;
|
private readonly IRequestContext _requestContext;
|
||||||
public AppDbContext(DbContextOptions<AppDbContext> options, IRequestContext requestContext)
|
public AppDbContext(DbContextOptions<AppDbContext> options, IRequestContext requestContext)
|
||||||
: base(options)
|
: base(options)
|
||||||
{
|
{
|
||||||
_requestContext = requestContext;
|
_requestContext = requestContext;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
#if DEBUG
|
#if !DEBUG
|
||||||
private dynamic _requestContext;
|
private dynamic _requestContext;
|
||||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||||
: base(options)
|
: base(options)
|
||||||
@@ -102,8 +99,8 @@ public AppDbContext(DbContextOptions<AppDbContext> options, IRequestContext requ
|
|||||||
{
|
{
|
||||||
((IAuditableEntity)entry.Entity).CreatedAt = utcNow;
|
((IAuditableEntity)entry.Entity).CreatedAt = utcNow;
|
||||||
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
||||||
((IAuditableEntity)entry.Entity).CreatedBy = _requestContext.CurrentUsername;
|
((IAuditableEntity)entry.Entity).CreatedBy = _requestContext.CurrentUsername ?? "Admin";
|
||||||
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername;
|
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername ?? "Admin";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void UpdateModifiedEntires(IEnumerable<EntityEntry> modifiedEntries, DateTime utcNow)
|
private void UpdateModifiedEntires(IEnumerable<EntityEntry> modifiedEntries, DateTime utcNow)
|
||||||
@@ -111,7 +108,7 @@ public AppDbContext(DbContextOptions<AppDbContext> options, IRequestContext requ
|
|||||||
foreach (var entry in modifiedEntries)
|
foreach (var entry in modifiedEntries)
|
||||||
{
|
{
|
||||||
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
||||||
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername;
|
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername ?? "Admin" ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void UpdateDeletedEntries(IEnumerable<EntityEntry> deletedEntries)
|
private void UpdateDeletedEntries(IEnumerable<EntityEntry> deletedEntries)
|
||||||
|
|||||||
Reference in New Issue
Block a user