Mentés + JwtMiddleware-ből a dolgok berakva
This commit is contained in:
@@ -11,7 +11,7 @@ namespace WorkFlowCheck.Web.Middleware
|
|||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
public readonly IConfiguration _configuration;
|
public readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public JwtMiddleware(RequestDelegate next,IConfiguration configuration)
|
public JwtMiddleware(RequestDelegate next, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_next = next;
|
_next = next;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
|
|||||||
@@ -1,99 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Identity.Data;
|
|
||||||
using Microsoft.IdentityModel.Tokens;
|
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using System.Text;
|
|
||||||
using WorkFlowCheck.Common.DTO;
|
|
||||||
|
|
||||||
namespace WorkFlowCheck.Web.Middleware
|
|
||||||
{
|
|
||||||
public class JwtService
|
|
||||||
{
|
|
||||||
|
|
||||||
private const string SecretKey = "super_secret_key";
|
|
||||||
public readonly IConfiguration _configuration;
|
|
||||||
private readonly HttpClient _httpClient;
|
|
||||||
|
|
||||||
public JwtService(HttpClient httpClient, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
_configuration = configuration;
|
|
||||||
_httpClient = httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<string?> AuthenticateUserAsync(UserDTO userDTO)
|
|
||||||
{
|
|
||||||
var jwtSettings = _configuration.GetSection("Jwt");
|
|
||||||
var secretKey = jwtSettings["SecretKey"];
|
|
||||||
var issuer = jwtSettings["Issuer"];
|
|
||||||
var audience = jwtSettings["Audience"];
|
|
||||||
var tokenLifetime = int.Parse(jwtSettings["TokenLifetimeMinutes"]);
|
|
||||||
|
|
||||||
var response = await Authenticate(userDTO.UserName, userDTO.Password);
|
|
||||||
if (response != null && response.IsSuccess)
|
|
||||||
{
|
|
||||||
|
|
||||||
var claims = new[]
|
|
||||||
{
|
|
||||||
new Claim(ClaimTypes.Name, userDTO.UserName),
|
|
||||||
new Claim(ClaimTypes.Role, "User")
|
|
||||||
};
|
|
||||||
|
|
||||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SecretKey));
|
|
||||||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
|
||||||
var token = new JwtSecurityToken(
|
|
||||||
issuer: issuer,
|
|
||||||
audience: audience,
|
|
||||||
claims: claims,
|
|
||||||
expires: DateTime.UtcNow.AddMinutes(tokenLifetime),
|
|
||||||
signingCredentials: creds);
|
|
||||||
|
|
||||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
private async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Az API végpont meghatározása
|
|
||||||
string endpoint = $"{_httpClient.BaseAddress}api/user/authenticate";
|
|
||||||
|
|
||||||
var userDTO = new UserDTO()
|
|
||||||
{
|
|
||||||
Email = "",
|
|
||||||
FirstName = "",
|
|
||||||
LastName = "",
|
|
||||||
Id = 0,
|
|
||||||
UserName = username,
|
|
||||||
Password = password,
|
|
||||||
RoleDTO = new List<RoleDTO>()
|
|
||||||
};
|
|
||||||
|
|
||||||
// HTTP POST kérés küldése
|
|
||||||
|
|
||||||
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userDTO))
|
|
||||||
{
|
|
||||||
httpResponseMessage.EnsureSuccessStatusCode();
|
|
||||||
|
|
||||||
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
|
||||||
var response = JsonConvert.DeserializeObject<ApiResponseDTO<UserDTO>>(jsonString);
|
|
||||||
|
|
||||||
return response ?? new ApiResponseDTO<UserDTO>
|
|
||||||
{
|
|
||||||
IsSuccess = false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
// Hiba visszaadása
|
|
||||||
return new ApiResponseDTO<UserDTO>
|
|
||||||
{
|
|
||||||
IsSuccess = false
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,9 +16,7 @@
|
|||||||
<div class="card shadow p-4">
|
<div class="card shadow p-4">
|
||||||
<form method="post" id="checkPointForm">
|
<form method="post" id="checkPointForm">
|
||||||
<meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
|
<meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
|
||||||
|
<input type="hidden" asp-for="CheckPoint.Id" />
|
||||||
<input type="hidden" id="hiddenCheckPointId" value="@Model.CheckPoint.Id" />
|
|
||||||
|
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label asp-for="CheckPoint.ShortName"></label>
|
<label asp-for="CheckPoint.ShortName"></label>
|
||||||
<input asp-for="CheckPoint.ShortName" class="form-control" />
|
<input asp-for="CheckPoint.ShortName" class="form-control" />
|
||||||
@@ -108,9 +106,6 @@
|
|||||||
$("#saveCheckPoint").click(function () {
|
$("#saveCheckPoint").click(function () {
|
||||||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||||
var formData = getFormAsNestedObject('#checkPointForm');
|
var formData = getFormAsNestedObject('#checkPointForm');
|
||||||
|
|
||||||
console.log(formData);
|
|
||||||
console.log(JSON.stringify(formData.CheckPoint));
|
|
||||||
|
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: $('#checkPointEditPostUrl').val(),
|
url: $('#checkPointEditPostUrl').val(),
|
||||||
|
|||||||
@@ -35,11 +35,17 @@ namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints
|
|||||||
|
|
||||||
public async Task<IActionResult> OnPostSave([FromBody] CheckPointDTO checkPointDTO)
|
public async Task<IActionResult> OnPostSave([FromBody] CheckPointDTO checkPointDTO)
|
||||||
{
|
{
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = await _baseStockService.UpdateCheckPoint(checkPointDTO);
|
var result = await _baseStockService.UpdateCheckPoint(checkPointDTO);
|
||||||
return new JsonResult(new { success = true });
|
if (result.IsSuccess)
|
||||||
|
{
|
||||||
|
return new JsonResult(new { success = true });
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new JsonResult(new { success = false });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ builder.Services.AddScoped<IBaseStockService, BaseStockService>();
|
|||||||
builder.Host.UseSerilog();
|
builder.Host.UseSerilog();
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddRazorPages();
|
builder.Services.AddRazorPages();
|
||||||
builder.Services.AddSingleton<JwtService>();
|
|
||||||
builder.Services.AddAntiforgery(options =>
|
builder.Services.AddAntiforgery(options =>
|
||||||
{
|
{
|
||||||
options.HeaderName = "X-CSRF-TOKEN"; // JS API hívásokhoz
|
options.HeaderName = "X-CSRF-TOKEN"; // JS API hívásokhoz
|
||||||
|
|||||||
@@ -1,23 +1,33 @@
|
|||||||
|
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
namespace WorkFlowCheck.Web.Services
|
namespace WorkFlowCheck.Web.Services
|
||||||
{
|
{
|
||||||
|
|
||||||
public class BaseService
|
public class BaseService
|
||||||
{
|
{
|
||||||
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||||
public readonly HttpClient _httpClient;
|
public readonly HttpClient _httpClient;
|
||||||
public readonly IConfiguration _configuration;
|
public readonly IConfiguration _configuration;
|
||||||
|
|
||||||
|
|
||||||
public BaseService(HttpClient httpClient, IConfiguration configuration)
|
|
||||||
|
public BaseService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
|
_httpContextAccessor = httpContextAccessor;
|
||||||
|
|
||||||
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
|
var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból
|
||||||
_httpClient.BaseAddress = new Uri(apiBaseUrl);
|
_httpClient.BaseAddress = new Uri(apiBaseUrl);
|
||||||
|
|
||||||
var apiKey = configuration["ApiKey"];
|
var apiKey = configuration["ApiKey"];
|
||||||
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
|
_httpClient.DefaultRequestHeaders.Add("X-Api-Key", apiKey);
|
||||||
|
|
||||||
|
var jwtToken = _httpContextAccessor.HttpContext?.Request.Cookies["AuthToken"];
|
||||||
|
if (!string.IsNullOrEmpty(jwtToken))
|
||||||
|
{
|
||||||
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Serilog;
|
using Newtonsoft.Json;
|
||||||
|
using Serilog;
|
||||||
using WorkFlowCheck.Common.DTO;
|
using WorkFlowCheck.Common.DTO;
|
||||||
using WorkFlowCheck.Web.Services.Interfaces;
|
using WorkFlowCheck.Web.Services.Interfaces;
|
||||||
|
|
||||||
@@ -6,7 +7,7 @@ namespace WorkFlowCheck.Web.Services
|
|||||||
{
|
{
|
||||||
public class BaseStockService : BaseService, IBaseStockService
|
public class BaseStockService : BaseService, IBaseStockService
|
||||||
{
|
{
|
||||||
public BaseStockService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration)
|
public BaseStockService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,9 +56,33 @@ namespace WorkFlowCheck.Web.Services
|
|||||||
return retVal;
|
return retVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CheckPointDTO> UpdateCheckPoint(CheckPointDTO checkPointDto)
|
public async Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint(CheckPointDTO checkPointDTO)
|
||||||
{
|
{
|
||||||
return null;
|
try
|
||||||
|
{
|
||||||
|
string endpoint = $"{_httpClient.BaseAddress}api/sync/updatecheckpoint";
|
||||||
|
|
||||||
|
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, checkPointDTO))
|
||||||
|
{
|
||||||
|
httpResponseMessage.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
||||||
|
var response = JsonConvert.DeserializeObject<ApiResponseDTO<CheckPointDTO>>(jsonString);
|
||||||
|
|
||||||
|
return response ?? new ApiResponseDTO<CheckPointDTO>
|
||||||
|
{
|
||||||
|
IsSuccess = false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Hiba visszaadása
|
||||||
|
return new ApiResponseDTO<CheckPointDTO>
|
||||||
|
{
|
||||||
|
IsSuccess = false
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,6 @@ namespace WorkFlowCheck.Web.Services.Interfaces
|
|||||||
{
|
{
|
||||||
Task<List<CheckPointDTO>> GetAllCheckPoints();
|
Task<List<CheckPointDTO>> GetAllCheckPoints();
|
||||||
Task<CheckPointDTO> GetCheckPoint(int id);
|
Task<CheckPointDTO> GetCheckPoint(int id);
|
||||||
Task<CheckPointDTO> UpdateCheckPoint(CheckPointDTO checkPoint);
|
Task<ApiResponseDTO<CheckPointDTO>> UpdateCheckPoint(CheckPointDTO checkPoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace WorkFlowCheck.Web.Services
|
|||||||
{
|
{
|
||||||
public class UserService : BaseService, IUserService
|
public class UserService : BaseService, IUserService
|
||||||
{
|
{
|
||||||
public UserService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration)
|
public UserService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
|
public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
|
||||||
|
|||||||
Reference in New Issue
Block a user