From 2247031e11835b56c4de3d2fe9ab55a6a8206316 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sat, 18 Nov 2023 16:24:32 +0100 Subject: [PATCH] changed: migrated logout page to razor components --- Components/Pages/Logout.razor | 19 +++++++++++++++++++ Pages/Logout.cshtml | 8 -------- Pages/Logout.cshtml.cs | 13 ------------- Program.cs | 1 + 4 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 Components/Pages/Logout.razor delete mode 100644 Pages/Logout.cshtml delete mode 100644 Pages/Logout.cshtml.cs diff --git a/Components/Pages/Logout.razor b/Components/Pages/Logout.razor new file mode 100644 index 0000000..3d8197a --- /dev/null +++ b/Components/Pages/Logout.razor @@ -0,0 +1,19 @@ +@page "/logout" +@using Microsoft.AspNetCore.Authentication +@using Microsoft.AspNetCore.Authentication.Cookies + +@inject IHttpContextAccessor HttpContextAccessor; +@inject NavigationManager Navigation; + +Logout - Just Short It + +

How did you get here?

+ +@code { + protected override async Task OnInitializedAsync() { + var context = HttpContextAccessor.HttpContext; + if (context is null) throw new ArgumentException("HttpContext"); + await context.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + Navigation.NavigateTo("/", true); + } +} diff --git a/Pages/Logout.cshtml b/Pages/Logout.cshtml deleted file mode 100644 index 353898e..0000000 --- a/Pages/Logout.cshtml +++ /dev/null @@ -1,8 +0,0 @@ -@page -@model JustShortIt.Pages.LogoutModel -@{ - ViewData["Title"] = "Logout"; -} - -

How did you get here?

- diff --git a/Pages/Logout.cshtml.cs b/Pages/Logout.cshtml.cs deleted file mode 100644 index 84f8a56..0000000 --- a/Pages/Logout.cshtml.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; - -namespace JustShortIt.Pages; - -public class LogoutModel : PageModel { - public async Task OnGetAsync() { - await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); - return LocalRedirect("~/"); - } -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index f45d01d..05c2afe 100644 --- a/Program.cs +++ b/Program.cs @@ -55,6 +55,7 @@ }); builder.Services.AddScoped(); builder.Services.AddCascadingAuthenticationState(); +builder.Services.AddHttpContextAccessor(); var app = builder.Build();