title: Failing to perform Cookie Authentication: SignInAsync and AuthenticateAsync not successful
body: <p>I am trying to build a very simple playground server for me to study some ASP.NET Core authentication/authorization concepts. Basically a web app with a single, very simple controller, to be tested with Postman.</p>
  <p>I came up with a minified version of my code, consisting of a single login endpoint which would authenticate the user (no credentials required) using Cookie Authentication, like that:</p>
  <pre><code>[ApiController]
  public class MyController : ControllerBase
  {
      [HttpGet(&quot;/login&quot;)]
      public async Task&lt;IActionResult&gt; Login()
      {
          var claims = new[] { new Claim(&quot;name&quot;, &quot;bob&quot;) };
          var identity = new ClaimsIdentity(claims);
          var principal = new ClaimsPrincipal(identity);
  
          await HttpContext.SignInAsync(principal);
          return Ok();
      }
  }
  </code></pre>
  <p>The thing is that the call to <code>HttpContext.SignInAsync()</code> is firing the following exception:</p>
  <pre><code>System.InvalidOperationException: SignInAsync when principal.Identity.IsAuthenticated is false is not allowed when AuthenticationOptions.RequireAuthenticatedSignIn is true.
     at Microsoft.AspNetCore.Authentication.AuthenticationService.SignInAsync(HttpContext context, String scheme, ClaimsPrincipal principal, AuthenticationProperties properties)
     at MyController.Login() in C:\Users\vinic\Desktop\TEMP\TestesAuthorization\Controllers\MyController.cs:line 18
  </code></pre>
  <p>Then I tried to replace <code>HttpContext.SignInAsync()</code> by a call to <code>HttpContext.AuthenticateAsync()</code>, so that I could authenticate the user before trying to call <code>SignInAsync()</code> again:</p>
  <pre><code>[HttpGet(&quot;/login&quot;)]
  public async Task&lt;IActionResult&gt; Login()
  {
      var authResult = await HttpContext.AuthenticateAsync();
      if (authResult.Succeeded == false)
          return StatusCode(500, &quot;Failed to autenticate!&quot;);
      return Ok();
  }
  </code></pre>
  <p>But in that case the <code>AuthenticateAsync()</code> result always returns a failure (<code>authResult.Succeeded</code> = false), and later calls to <code>HttpContext.SignInAsync()</code> would fail with the same InvalidOperationException as before. By enabling &quot;Trace&quot;-level logging, the call to AuthenticateAsync() only logs the following (not very helpful) piece of information:</p>
  <pre><code>dbug: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[9]
        AuthenticationScheme: Cookies was not authenticated.
  Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler: Debug: AuthenticationScheme: Cookies was not authenticated.
  </code></pre>
  <p>My project targets the <code>net5.0</code> framework, has no external/explicit dependencies, and here's the Startup class I'm using:</p>
  <pre><code>public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddControllers();
          services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
              .AddCookie();
      }
  
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configs)
      {
          app.UseRouting();
          app.UseAuthentication();
          app.UseEndpoints(endpoints =&gt;
          {
              endpoints.MapControllers();
          });
      }
  }
  </code></pre>
  <p>I know I must be missing something really basic here. I'm also not sure if <a href="https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-5.0" rel="noreferrer">the documentation</a> I am basing myself on is actually up-to-date for .NET 5.0.</p>
  <p>Why is the cookie authentication (<code>HttpContext.SignInAsync()</code> / <code>HttpContext.AuthenticateAsync()</code>) failing?</p>