From 3091faa94c4540751aad9f8fc2058fab5243f2b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Szab=C3=B3?= Date: Wed, 12 Mar 2025 15:00:37 +0100 Subject: [PATCH] =?UTF-8?q?Most=20m=C5=B1k=C3=B6dik=20alapb=C3=B3l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CheckPoints/CheckPointEditPage.cshtml.cs | 2 ++ src/WorkFlowCheck.Web/Pages/Index.cshtml.cs | 2 ++ src/WorkFlowCheck.Web/Program.cs | 24 +++++-------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs index f3784e3..dc7206a 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using WorkFlowCheck.Common.DTO; @@ -5,6 +6,7 @@ using WorkFlowCheck.Web.Services.Interfaces; namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints { + [Authorize] public class CheckPointEditPageModel : PageModel { private readonly ILogger _logger; diff --git a/src/WorkFlowCheck.Web/Pages/Index.cshtml.cs b/src/WorkFlowCheck.Web/Pages/Index.cshtml.cs index 5d03c67..11c1aa9 100644 --- a/src/WorkFlowCheck.Web/Pages/Index.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/Index.cshtml.cs @@ -1,8 +1,10 @@ +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace WorkFlowCheck.Web.Pages { + //[Authorize] public class IndexModel : PageModel { private readonly ILogger _logger; diff --git a/src/WorkFlowCheck.Web/Program.cs b/src/WorkFlowCheck.Web/Program.cs index 4869e3b..1f5e4d4 100644 --- a/src/WorkFlowCheck.Web/Program.cs +++ b/src/WorkFlowCheck.Web/Program.cs @@ -26,6 +26,9 @@ builder.Host.UseSerilog(); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddSingleton(); + +var jwtSettings = builder.Configuration.GetSection("Jwt"); + builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { @@ -37,9 +40,9 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, - ValidIssuer = "https://auth.example.com", - ValidAudience = "https://mywebapp.com", - IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("super_secret_key")) + ValidIssuer = jwtSettings["Issuer"], + ValidAudience = jwtSettings["Audience"], + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["SecretKey"])) }; }); @@ -62,21 +65,6 @@ app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); -app.MapPost("/login", async ([FromBody] UserDTO userDTO, JwtService jwtService, HttpContext httpContext) => -{ - var token = await jwtService.AuthenticateUserAsync(userDTO); - if (token is null) return Results.Unauthorized(); - - httpContext.Response.Cookies.Append("AuthToken", token, new CookieOptions - { - HttpOnly = true, - Secure = true, - SameSite = SameSiteMode.Strict - }); - return Results.Ok(new { Token = token }); -}); - -app.MapGet("/secure-data", [Authorize] () => "This is protected data"); app.Use(async (context, next) => { if (string.IsNullOrEmpty(context.User?.Identity?.Name) &&