Logout with Identity

I'm trying to use the logoff function offered by default in Identity:

    // POST: /Account/LogOff
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LogOff()
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("Index", "Home");

But every time I try to call this function it returns this error:

Server Error in application'/'.

And says the requested URL was:

URL requested:/Manage / LogOff

To call this Action I used the following method:

 @if (User.Identity.IsAuthenticated) {<li>@Html.ActionLink("Sair", "LogOff", "AccountController", FormMethod.Post)</li> }

Instead of calling AccountController I have tried to use only Account as described in the function comment // POST: / Account / LogOff, but I did not succeed.

To make this work should I call it another way ? or write a different method to perform the logoff ?

Author: Prostetnic Vogon Jeltz, 2017-10-04

3 answers

The helper @Html.ActionLink generates an HTML element <a> - anchor. The requests made from it are of type GET, not allowing the action that has Type POST to be executed.

Try changing the annotation HttpPost to HttpGet or change your call to perform a POST request.

Ex:

[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("Index", "Home");
 1
Author: Good Bye Blue sky, 2017-10-04 18:08:13

I implemented one of these yesterday, I believe the controller method needs to be a post to request the logoff:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Logoff()
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        return RedirectToAction("Login", "Account");
    }

On the front-end, I use HtmlHelper BeginForm to already mount the page with the Logoff option available if the request is authenticated, specifying that I want a post method of the controller:

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("Logoff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink($"Oi {User.Identity.GetUserName()}!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Registrar Usuário", "RegisterUser", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}
 2
Author: James Braz, 2017-10-04 20:39:52

Follows example of call on front-end:

   <ul>
      <li>
          <a href="~/Login/Logout">
          <i class="ace-icon fa fa-power-off"></i>
             Sair
          </a>                               
      </li>
  </ul>

Follows example of LogOut function:

public ActionResult Logout()
    {

        HttpContext.GetOwinContext()
                   .Authentication
                   .SignOut(HttpContext.GetOwinContext()
                                       .Authentication.GetAuthenticationTypes()
                                       .Select(o => o.AuthenticationType).ToArray());

        return RedirectToAction("Index");
    }
 1
Author: Alexandre Cavaloti, 2017-10-04 18:11:12