From c9ffad0d91d7ad2341b7c39b57e215f9c774d06d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Szab=C3=B3?= Date: Wed, 12 Mar 2025 10:38:30 +0100 Subject: [PATCH] =?UTF-8?q?Van=20norm=C3=A1lis=20bejelenkez=C3=A9si=20abla?= =?UTF-8?q?k?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Middleware/JwtMiddleware.cs | 15 ++++-- .../Middleware/JwtService.cs | 1 + .../Pages/Account/Login.cshtml | 50 +++++++++++++++++++ .../Pages/Account/Login.cshtml.cs | 25 ++++++++++ .../CheckPoints/CheckPointsPage.cshtml.cs | 2 + src/WorkFlowCheck.Web/Program.cs | 11 ++++ .../Services/Interfaces/IUserService.cs | 6 +++ src/WorkFlowCheck.Web/Services/UserService.cs | 12 +++++ 8 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 src/WorkFlowCheck.Web/Pages/Account/Login.cshtml create mode 100644 src/WorkFlowCheck.Web/Pages/Account/Login.cshtml.cs create mode 100644 src/WorkFlowCheck.Web/Services/Interfaces/IUserService.cs create mode 100644 src/WorkFlowCheck.Web/Services/UserService.cs diff --git a/src/WorkFlowCheck.Web/Middleware/JwtMiddleware.cs b/src/WorkFlowCheck.Web/Middleware/JwtMiddleware.cs index bfd8e6e..6724ebc 100644 --- a/src/WorkFlowCheck.Web/Middleware/JwtMiddleware.cs +++ b/src/WorkFlowCheck.Web/Middleware/JwtMiddleware.cs @@ -1,4 +1,5 @@ -using Microsoft.IdentityModel.Tokens; +using Microsoft.Extensions.Configuration; +using Microsoft.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using System.Text; @@ -8,11 +9,12 @@ namespace WorkFlowCheck.Web.Middleware public class JwtMiddleware { private readonly RequestDelegate _next; - private const string _secretKey = "super_secret_key"; + public readonly IConfiguration _configuration; - public JwtMiddleware(RequestDelegate next) + public JwtMiddleware(RequestDelegate next,IConfiguration configuration) { _next = next; + _configuration = configuration; } public async Task Invoke(HttpContext context) @@ -26,12 +28,14 @@ namespace WorkFlowCheck.Web.Middleware 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); + var key = Encoding.UTF8.GetBytes(secretKey); try { @@ -52,5 +56,6 @@ namespace WorkFlowCheck.Web.Middleware return false; } } + } } diff --git a/src/WorkFlowCheck.Web/Middleware/JwtService.cs b/src/WorkFlowCheck.Web/Middleware/JwtService.cs index b689a35..69f45cb 100644 --- a/src/WorkFlowCheck.Web/Middleware/JwtService.cs +++ b/src/WorkFlowCheck.Web/Middleware/JwtService.cs @@ -94,5 +94,6 @@ namespace WorkFlowCheck.Web.Middleware }; } } + } } diff --git a/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml b/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml new file mode 100644 index 0000000..7718b64 --- /dev/null +++ b/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml @@ -0,0 +1,50 @@ +@page +@model WorkFlowCheck.Web.Pages.Account.LoginModel +@{ + Layout = null; + ViewData["Title"] = "Bejelentkezés"; +} + + + + + + + + + @ViewData["Title"] + + + +
+

Bejelentkezés

+ + @if (!string.IsNullOrEmpty(Model.ErrorMessage)) + { +
@Model.ErrorMessage
+ } + +
+
+ + + +
+ +
+ + + +
+ +
+ +
+
+
+ + + + + + diff --git a/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml.cs b/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml.cs new file mode 100644 index 0000000..14c1b7d --- /dev/null +++ b/src/WorkFlowCheck.Web/Pages/Account/Login.cshtml.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using WorkFlowCheck.Common.DTO; +using WorkFlowCheck.Web.Services.Interfaces; + +namespace WorkFlowCheck.Web.Pages.Account +{ + public class LoginModel : PageModel + { + + private readonly IUserService _userService; + [BindProperty] + public UserDTO UserDTO { get; set; } + public string ErrorMessage { get; set; } + + public LoginModel(IUserService userService) + { + _userService = userService; + } + + public void OnGet() + { + } + } +} diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml.cs b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml.cs index 9a549a4..9005fa2 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.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 CheckPointsPageModel : PageModel { private readonly ILogger _logger; diff --git a/src/WorkFlowCheck.Web/Program.cs b/src/WorkFlowCheck.Web/Program.cs index 9f54edb..4869e3b 100644 --- a/src/WorkFlowCheck.Web/Program.cs +++ b/src/WorkFlowCheck.Web/Program.cs @@ -19,6 +19,7 @@ builder.Services.AddMemoryCache(); builder.Services.AddHttpContextAccessor(); builder.Services.AddHttpClient(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Host.UseSerilog(); @@ -76,7 +77,17 @@ app.MapPost("/login", async ([FromBody] UserDTO userDTO, JwtService jwtService, }); app.MapGet("/secure-data", [Authorize] () => "This is protected data"); +app.Use(async (context, next) => +{ + if (string.IsNullOrEmpty(context.User?.Identity?.Name) && + context.Request.Path == "/") + { + context.Response.Redirect("/Account/Login"); + return; + } + await next(); +}); app.MapRazorPages(); app.Run(); diff --git a/src/WorkFlowCheck.Web/Services/Interfaces/IUserService.cs b/src/WorkFlowCheck.Web/Services/Interfaces/IUserService.cs new file mode 100644 index 0000000..471dac8 --- /dev/null +++ b/src/WorkFlowCheck.Web/Services/Interfaces/IUserService.cs @@ -0,0 +1,6 @@ +namespace WorkFlowCheck.Web.Services.Interfaces +{ + public interface IUserService + { + } +} diff --git a/src/WorkFlowCheck.Web/Services/UserService.cs b/src/WorkFlowCheck.Web/Services/UserService.cs new file mode 100644 index 0000000..0415b82 --- /dev/null +++ b/src/WorkFlowCheck.Web/Services/UserService.cs @@ -0,0 +1,12 @@ + +using WorkFlowCheck.Web.Services.Interfaces; + +namespace WorkFlowCheck.Web.Services +{ + public class UserService : BaseService, IUserService + { + public UserService(HttpClient httpClient, IConfiguration configuration) : base(httpClient, configuration) + { + } + } +}