How do I update a claim value in an ASP.NET Core 2.1 Authentication Cookie?
Little bit of context to my question:
I have an dotnet Core 2.1 MVC project that is using Cookie authentication.
The web site gets most of it's data from a API that uses bearer tokens to authenticate. The bearer tokens are fairly short lived and the API also returns a refresh token to get a new bearer token.
When the user logs in I get a token from the API and store it as a claim in the auth cookie, like so:
var props = new AuthenticationProperties{ ... };
var claims = new[]
{
new Claim("ApiAccessToken", apiToken.AccessToken),
new Claim("ApiRefreshToken", apiToken.RefreshToken),
// other claims ...
};
var identity = new ClaimsIdentity(claims,
CookieAuthenticationDefaults.AuthenticationScheme);
await Request.HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity), props);
I'm using Polly to ensure authentication before making calls to the API, if the first try to the API gets a 401, I use the refresh token to get a new bearer token. I want to update the ApiAccessToken and ApiRefreshToken claims in the auth cookie with these new values.
Or is there a altogether better way to handle this scenario?
Thanks.