1

I am making a website to simply administrate but I cannot manage to log the user out, I am using cookies to verify the user. When I try to asynchronously log the user out it says "page not found". This is what ive come up with:

[HttpPost]
public async Task<IActionResult> SignOutAsync()
{
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    
    return RedirectToAction("Index");
}

And in my .cshtml file:

@if (User.Identity.IsAuthenticated)
{
    <p>Hello, @User.Identity.Name</p>
    <a onclick="document.getElementById('logout_form').submit();" style="cursor: pointer;">
        Sign out
    </a>
    <form asp-controller="Home" asp-action="SignOutAsync" method="post" id="logout_form"></form>
}
else
{
    <a asp-controller="Home" asp-action="Login">Sign in</a>
}
Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
MaxK123
  • 390
  • 3
  • 15

1 Answers1

0

Give this a try? The page not found because you need to add the redirect in the method.

Do you need a post for the logout? Would a get not suffice?

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme, new AuthenticationProperties
        { 
            RedirectUri = Url.Action("Index","Home")
        });
Igor
  • 298
  • 3
  • 8