title: Logout Action with Asp.Net Core Cookie Authentication
body: <p>I have implemented authentication in Asp.Net Core 2.2 like this:</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; LoginAsync(string user, string password)
      {
          if (user == "admin" &amp;&amp; password == "admin")
          {
              var claims = new[] { new Claim(ClaimTypes.Name, user),
              new Claim(ClaimTypes.Role, "Admin") };
  
              var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
  
              await HttpContext.SignInAsync(
                  CookieAuthenticationDefaults.AuthenticationScheme,
                  new ClaimsPrincipal(identity));
  
              return RedirectToAction("Index", "Home");
          {
          else
          {
              return RedirectToAction("Login", "Users");
          }
  </code></pre>
  
  <p>I need to make a Logout action now. I used to achieve this in Asp.Net MVC with FormsAuthentication.SignOut()... I need to know the proper way to do it in Asp.Net Core 2.2</p>
  
  <p>What I've tried is to make a Logout action like this:</p>
  
  <pre><code>    public async Task&lt;IActionResult&gt; Logout()
      {
          await HttpContext.SignOutAsync();
          return RedirectToAction("Index","Home");
      }
  </code></pre>
  
  <p>And used the following code in my NavBar:</p>
  
  <pre><code>@if (User.Identity.IsAuthenticated)
              {
                  using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
                  {
                      @Html.AntiForgeryToken()
  
                      &lt;ul class="nav navbar-nav navbar-right"&gt;
                          &lt;li&gt;
                              @Html.ActionLink("Hello " + User.Identity.Name + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
                          &lt;/li&gt;
                          &lt;li class="nav-item"&gt;
                              &lt;form class="form-inline" asp-area="Identity" asp-page="/Users/Logout" asp-route-returnUrl="@Url.Action("Index", "Home", new { area = "" })"&gt;
                                  &lt;button type="submit" class="nav-link btn btn-link text-dark"&gt;Logout&lt;/button&gt;
                              &lt;/form&gt;
                          &lt;/li&gt;
                      &lt;/ul&gt;
                  }
              }
              else
              {
                  &lt;ul class="nav navbar-nav navbar-right"&gt;
                      &lt;li&gt;@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })&lt;/li&gt;
                      &lt;li&gt;@Html.ActionLink("Log in", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })&lt;/li&gt;
                  &lt;/ul&gt;
              }
  </code></pre>
  
  <p>Following instructions from this <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.2&amp;tabs=visual-studio#log-out" rel="noreferrer">documentaion</a></p>
  
  <p>This correctly shows the Logout button, but pressing the button doesn't seem to trigger my action, and the user is not logged out.</p>