Skip to content

Commit

Permalink
switch to razor pages for logout, so it actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Dec 12, 2024
1 parent 29c47ec commit f36c542
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
26 changes: 0 additions & 26 deletions src/web/Jordnaer/Components/Account/Pages/Logout.razor

This file was deleted.

27 changes: 27 additions & 0 deletions src/web/Jordnaer/Pages/Account/Logout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@page

@model Jordnaer.Components.Account.Pages.LogoutModel

@using Microsoft.AspNetCore.Authorization

@attribute [Authorize]

<form method="post">
@Html.AntiForgeryToken()
<input type="hidden" name="returnUrl" value="@Model.ReturnUrl"/>
<button type="submit" class="nav-link">
<span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span>
</button>
</form>

<div class="container">
<div class="alert alert-info text-center" role="alert">
Du er nu logget ud.
</div>
</div>

<script>
window.onload = () => {
document.forms[0].submit();
}
</script>
25 changes: 25 additions & 0 deletions src/web/Jordnaer/Pages/Account/Logout.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Jordnaer.Database;
using Jordnaer.Features.Metrics;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Jordnaer.Components.Account.Pages;

public class LogoutModel(SignInManager<ApplicationUser> signInManager) : PageModel
{
[BindProperty(SupportsGet = true)]
public string? ReturnUrl { get; set; }
public void OnGet(){}

public async Task<IActionResult> OnPostAsync()
{
await HttpContext.SignOutAsync();
await signInManager.SignOutAsync();

JordnaerMetrics.LogoutCounter.Add(1);

return LocalRedirect(ReturnUrl ?? "~/");
}
}
4 changes: 4 additions & 0 deletions src/web/Jordnaer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
.AddHttpContextAccessor()
.AddDefaultSitemapServices<HttpContextBaseUrlProvider>();

builder.Services.AddRazorPages();

var app = builder.Build();

app.UseSecurityHeaders(policies => policies.AddFrameOptionsDeny()
Expand Down Expand Up @@ -134,6 +136,8 @@
app.UseAuthorization();
app.UseAntiforgery();

app.MapRazorPages();

app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();

Expand Down

0 comments on commit f36c542

Please sign in to comment.