Search code examples
postblazorlogout

OnPost not called when trying to log out


I have a Blazor app where I sucessfully log out with a GET request.

https://localhost:44300/Identity/Account/LogOut

public class LogoutModel : PageModel
{
    public async Task<IActionResult> OnGetAsync()
    {
        await HttpContext.SignOutAsync();
        return Redirect("/Identity/Account/Unauthorized");
    }
}

However with good reason this GET should be a POST.
So I changed the code to:

public class LogoutModel : PageModel
{
    public async Task<IActionResult> OnPostAsync()
    {
        await HttpContext.SignOutAsync();
        return Redirect("/Identity/Account/Unauthorized");
    }
}

The OnPostAsync() method is not invoked and I get an 400 response when I call the following fetch method.

await fetch('https://localhost:44300/Identity/Account/LogOut', { method: 'POST', headers: { 'Content-type': 'application/json; charset=UTF-8' }})

What do I need to do to ensure it get's called?


Solution

  • The request verification was failing, so I disabled it by adding the IgnoreAntiforgeryToken attribute to the LogoutModel class:

    [IgnoreAntiforgeryToken(Order = 1001)]
    public class LogoutModel : PageModel
    {
    }