From 2cf66395fe433d2e6a13fa99e6285f6afba09f75 Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 23 Aug 2024 20:44:57 -0500 Subject: [PATCH] Add minimal auth --- Pages/Login.cshtml | 5 +++++ Pages/NewDefinition.cshtml.cs | 8 +++++++- Program.cs | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Pages/Login.cshtml diff --git a/Pages/Login.cshtml b/Pages/Login.cshtml new file mode 100644 index 0000000..ba5ac7d --- /dev/null +++ b/Pages/Login.cshtml @@ -0,0 +1,5 @@ +@page + +

+ :D (login) +

\ No newline at end of file diff --git a/Pages/NewDefinition.cshtml.cs b/Pages/NewDefinition.cshtml.cs index 26f45ef..7b274be 100644 --- a/Pages/NewDefinition.cshtml.cs +++ b/Pages/NewDefinition.cshtml.cs @@ -5,9 +5,15 @@ namespace JergueroCS.Pages { public class NewDefinitionModel : PageModel { - public void OnGet() + public IActionResult OnGet() { + if (User.Identity?.IsAuthenticated != true) + { + return RedirectToPage("/Login"); + } + System.Console.WriteLine(":D"); + return Page(); } } } diff --git a/Program.cs b/Program.cs index bc275e4..db910bf 100644 --- a/Program.cs +++ b/Program.cs @@ -1,7 +1,16 @@ +using Microsoft.AspNetCore.Authentication.Cookies; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); +builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(options => + { + options.ExpireTimeSpan = TimeSpan.FromMinutes(20); + options.SlidingExpiration = true; + options.AccessDeniedPath = "/Forbidden/"; + }); var app = builder.Build(); @@ -18,6 +27,11 @@ app.UseStaticFiles(); app.UseRouting(); +app.UseCookiePolicy(new CookiePolicyOptions +{ + MinimumSameSitePolicy = SameSiteMode.Strict, +}); +app.UseAuthentication(); app.UseAuthorization(); app.MapRazorPages();