title: ASP.NET Core - HttpContext.User.Identity.Name is null in controller
body: <p>I am using a custom <code>CookieAuthentication</code> in ASP.NET Core and I have these three methods in my HomeController:</p>
  
  <pre><code>[Authorize]
  public class HomeController : Controller
  {
      public async Task&lt;IActionResult&gt; Index()
      {
          _logger.LogInformation("Index()");
          if (HttpContext.User.Identity.Name != null)
          {
              _logger.LogInformation(User.Identity.Name);
          }
          ...
      }
  
      [HttpGet("getlastdisposal")]
      [AllowAnonymous]
      public JsonResult GetLastDisposal()
      {
          _logger.LogInformation("GetLastDisposal()");
          if (HttpContext.User.Identity.Name != null)
          {
              _logger.LogInformation(User.Identity.Name);
          }
          else
          {
              _logger.LogInformation("IT IS NULL");
          }
          ...
      }
  
      [HttpGet("getchartdata")]
      public JsonResult GetChartData()
      {
          ...
      }
  }
  </code></pre>
  
  <p>The log I am getting back is this:</p>
  
  <pre><code>...
  info: Server.Controllers.HomeController[0]
        Index()
  info: Server.Controllers.HomeController[0]
        admin
  ...
  info: Server.Controllers.HomeController[0]
        GetLastDisposal()
  info: Server.Controllers.HomeController[0]
        IT IS NULL
  ...
  info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
        Authorization failed for user: (null).
  info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
        Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
  info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
        Executing ChallengeResult with authentication schemes ().
  info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware[12]
        AuthenticationScheme: Cookies was challenged.
  </code></pre>
  
  <p>Looking at this, it seems like <code>HttpContext.User.Identity.Name</code> becomes null after the first successful authorization (before <code>Index()</code>), which makes the authorization before <code>GetChartData()</code> fail. Any ideas as to why this is happening and how I can fix it?</p>