Logout with Identity

2

I am 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:

  

Application Server Error '/'.

And says the requested URL was:

  

Requested URL: / 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 comment of the // POST: / Account / LogOff function, but I did not succeed.

To make this work, should I call it another way? or write a different method to log off?

    
asked by anonymous 04.10.2017 / 19:58

3 answers

1

The @Html.ActionLink vel helper 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 make a POST-type request.

Ex:

[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("Index", "Home");
    
04.10.2017 / 20:08
2

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

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

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

@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>
}
    
04.10.2017 / 22:39
1

Here is a sample call on the front end:

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

Following example of LogOut function:

public ActionResult Logout()
    {

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

        return RedirectToAction("Index");
    }
    
04.10.2017 / 20:05