title: ASP.NET Identity user null after login
body: <h2>Edit 1</h2>
  
  <p>Having updated the code to better handle this problem, I'm now running into the following problem:</p>
  
  <blockquote>
    <p>The provided anti-forgery token was meant for a different claims-based user than the current user.</p>
  </blockquote>
  
  <p>Here's the updated code:</p>
  
  <pre><code>var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
  if (result == SignInStatus.Success)
  {
      var store = new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext());
      var manager = new UserManager&lt;ApplicationUser&gt;(store);
      var user = manager.FindById(User.Identity.GetUserId());
      GenerateUserCookie(model, user, returnUrl);
  }
  switch (result)
  {
      case SignInStatus.LockedOut:
          return View("Lockout");
      case SignInStatus.RequiresVerification:
          return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
      case SignInStatus.Failure:
          ModelState.AddModelError("", "Invalid login attempt.");
          return View(model);
      default:
          return View(model);
  }
  
  private ActionResult GenerateUserCookie(LoginViewModel model, ApplicationUser user, string returnUrl)
  {
      if (user == null)
      {
          ModelState.AddModelError("", "Someething went wrong; please try logging in again.");
          return View(model);
      }
  
      var cookie = new HttpCookie("stman");
      if (user.AgentId &gt;= 1) cookie.Values.Add("aid", user.AgentId.ToString());
  
      cookie.Values.Add("wumpus", user.Id.ToString());
      Response.Cookies.Add(cookie);
      return RedirectToLocal(returnUrl);
  }
  </code></pre>
  
  <h2>Original Question</h2>
  
  <p>Like the title says, after I log in, the user value returned by the following code is still null.</p>
  
  <p>To actually get a user, I have to open the login screen again (not refresh) and log in again...</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
  {
      if (!ModelState.IsValid)
      {
          return View(model);
      }
  
      // This doesn't count login failures towards account lockout
      // To enable password failures to trigger account lockout, change to shouldLockout: true
      var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
      switch (result)
      {
          case SignInStatus.Success:
              var store = new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext());
              var manager = new UserManager&lt;ApplicationUser&gt;(store);
              var user = manager.FindById(User.Identity.GetUserId());
              GenerateUserCookie(user);
              return RedirectToLocal(returnUrl);
          case SignInStatus.LockedOut:
              return View("Lockout");
          case SignInStatus.RequiresVerification:
              return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
          case SignInStatus.Failure:
          default:
              ModelState.AddModelError("", "Invalid login attempt.");
              return View(model);
      }
  }
  
  private void GenerateUserCookie(ApplicationUser user)
  {
      var cookie = new HttpCookie("stman");
  
      // Throws exception most times since the user is null.
      if (user.AgentId &gt;= 1)
      {
          cookie.Values.Add("aid", user.AgentId.ToString());
      }
      cookie.Values.Add("wumpus", user.Id.ToString());
      Response.Cookies.Add(cookie);
  }
  </code></pre>