Most működik alapból

This commit is contained in:
2025-03-12 15:00:37 +01:00
parent 7c1d89d783
commit 3091faa94c
3 changed files with 10 additions and 18 deletions
@@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using WorkFlowCheck.Common.DTO; using WorkFlowCheck.Common.DTO;
@@ -5,6 +6,7 @@ using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints
{ {
[Authorize]
public class CheckPointEditPageModel : PageModel public class CheckPointEditPageModel : PageModel
{ {
private readonly ILogger<IndexModel> _logger; private readonly ILogger<IndexModel> _logger;
@@ -1,8 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WorkFlowCheck.Web.Pages namespace WorkFlowCheck.Web.Pages
{ {
//[Authorize]
public class IndexModel : PageModel public class IndexModel : PageModel
{ {
private readonly ILogger<IndexModel> _logger; private readonly ILogger<IndexModel> _logger;
+6 -18
View File
@@ -26,6 +26,9 @@ 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.AddSingleton<JwtService>();
var jwtSettings = builder.Configuration.GetSection("Jwt");
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => .AddJwtBearer(options =>
{ {
@@ -37,9 +40,9 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
ValidateAudience = true, ValidateAudience = true,
ValidateLifetime = true, ValidateLifetime = true,
ValidateIssuerSigningKey = true, ValidateIssuerSigningKey = true,
ValidIssuer = "https://auth.example.com", ValidIssuer = jwtSettings["Issuer"],
ValidAudience = "https://mywebapp.com", ValidAudience = jwtSettings["Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("super_secret_key")) IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["SecretKey"]))
}; };
}); });
@@ -62,21 +65,6 @@ app.UseRouting();
app.UseAuthentication(); app.UseAuthentication();
app.UseAuthorization(); 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) => app.Use(async (context, next) =>
{ {
if (string.IsNullOrEmpty(context.User?.Identity?.Name) && if (string.IsNullOrEmpty(context.User?.Identity?.Name) &&