I have an ASP.NET Core API \ Angular app. My API needs to support cookies and tokens.
After I login using my service the anti-forgery token returned is not valid as it was created based on a null user. I've tried setting ClaimsPrincipal after my PasswordSignInAsync and regenerating the anti-forgery token (see below) but that still does not work. Any ideas?
public virtual async Task<IActionResult> Login([FromBody] AccountLoginModel model)
{
var result = await this.SignInManager.PasswordSignInAsync(model.Email, model.Password, isPersistent: model.RememberMe, lockoutOnFailure: false);
if (!result.Succeeded)
{
return this.BadRequest();
}
var user = await this.UserManager.FindByEmailAsync(model.Email);
// Must manually set the HttpContext user claims to those of the logged
// in user. Otherwise MVC will still include a XSRF token for the "null"
// user and token validation will fail. (MVC appends the correct token for
// all subsequent reponses but this isn't good enough for a single page
// app.)
var principal = await this.PrincipalFactory.CreateAsync(user);
this.HttpContext.User = principal;
// Update XSRF token
var tokens = this.Antiforgery.GetAndStoreTokens(this.HttpContext);
return this.Ok();
}