Logout com Identity

Asked

Viewed 948 times

3

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 :

Server Error in Application '/'.

And it 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 to Accountcontroller I tried to use only Account as described in the function comment // POST: /Account/Logoff, but I was not successful.

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

3 answers

2

I implemented one of these yesterday, I believe 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 Post controller 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>
}

1

Below is an example of a front-end call:

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

Follow example of Logout function:

public ActionResult Logout()
    {

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

        return RedirectToAction("Index");
    }
  • But to call this action on the front end, how would you do ? considering that it will be on the Accountcontroller ? because when I use @Html.Actionlink("Quit", "Logoff", "Accountcontroller", Formmethod.Post) it doesn’t work ?

  • Include the call on the front end in the example. Using the "Login controller"

1


The helper @Html.ActionLink generates an HTML element <a> - anchovy. The requisitions made from it are of the type GET, not enabling the action that has type POST be executed.

Try to change the note HttpPost for HttpGet or change your call to make a POST request.

Ex:

[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
    return RedirectToAction("Index", "Home");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.