title: ASP.NET Core authentication cookie only received once
body: <p>I am developing an application with ASP.NET Core and I am using a custom Cookie Authentication. My <code>CookieAuthenticationOptions</code> are:</p>
  
  <pre><code>app.UseCookieAuthentication(new CookieAuthenticationOptions()
  {
      AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme,
      LoginPath = new PathString("/login"),
      AccessDeniedPath = new PathString("/unauthorized/"),
      AutomaticAuthenticate = true,
      AutomaticChallenge = true
  });
  </code></pre>
  
  <p>The cookie is created just fine and I can see it in the browser settings throughout the whole time I am running the application. This is my <code>HomeController</code> class:</p>
  
  <pre><code>public HomeController(IHostingEnvironment env,
      IAntiforgery antiforgery,
      IOptions&lt;AppSettings&gt; appSettings,
      TerminalDbContext terminalContext,
      ILoggerFactory loggerFactory,
      IHttpContextAccessor _httpContextAccessor)
  {
      _env = env;
      _antiforgery = antiforgery;
      _appSettings = appSettings;
      _terminalContext = terminalContext;
      _logger = loggerFactory.CreateLogger&lt;HomeController&gt;();
      _httpContext = _httpContextAccessor.HttpContext;
  
  
      _logger.LogInformation("Cookie coming");
      var cookies = _httpContext.Request.Cookies[".AspNetCore.Cookies"];
      if (cookies != null)
      {
          _logger.LogInformation(cookies.Length.ToString());
          _logger.LogInformation(cookies.ToString());
      }
      else
      {
      _logger.LogInformation("THE COOKIE IS NULL");
      }
  }
  </code></pre>
  
  <p>And this is how I sign in the user:</p>
  
  <pre><code>var claims = new List&lt;Claim&gt;
      {
          new Claim(ClaimTypes.Name, loginInfo.Username),
          new Claim("DbName", loginInfo.Terminal.SesamDbName),
      };
  
  var userIdentity = new ClaimsIdentity(claims, "password");
  
  ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
  await _httpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
  </code></pre>
  
  <p>I am running the application and more than one instances of the <code>HomeController</code> are created, since I have <code>HttpGet</code> methods that return a <code>JsonResult</code> that is needed for the view.</p>
  
  <p>The first time the application tries to <code>[Authorize]</code> (for the <code>Index()</code> method), it finds the cookie and authenticates and authorizes fine. The second time it tries to <code>[Authorize]</code> (for an <code>HttpGet</code> method that returns a <code>JsonResult</code>) it doesn't find the cookie, even though it is there in my browser's settings. This is the log I get, to illustrate this:</p>
  
  <pre><code>...
  info: Server.Controllers.HomeController[0]
        Cookie coming
  info: Server.Controllers.HomeController[0]
        347
  info: Server.Controllers.HomeController[0]
    CfDJ8GSLZENXaNpNrtmz2DAt9joqJ6CEHpCFbJdbNxbQYjjoQmd4naOI0L0krNMSQdVhqPRP9tJJMMIRayc5ILRQMcJQWNZ0T9Fjuk7Qxg65wPP7SR43UZxwy6vGQ7_qeSp44gYLLe4NGEalhXynZxmD-jywqL4VJZ5y4OwpsEKLx-VVT03xAlt54J_qQk_O4wjwLQiZBpAVTFKUWN4u7H8yd_rwMTIGBPu21t5n35To9bTQU5677xNxiEFap3ukuxO4p-OxVakXqShy2Xk_vYDAvv_XFV6jgNcy4ZiCRB8VUhXGcNr205h4X0-O7JHB8mYbc13aZLmrAwvG5DWTBd3_OCo
  ...
  info: Server.Controllers.HomeController[0]
        Cookie coming
  info: Server.Controllers.HomeController[0]
        THE COOKIE IS NULL
  </code></pre>
  
  <p>Why does this happen? What can I do about it?</p>