Update Claims values in ASP.NET One Core

Martín picture Martín · Aug 18, 2016 · Viewed 10.3k times · Source

I have a Web Application in MVC 6 (Asp.Net One Core), and I'm using Claims based authentication. In the Login method I set the Claims:

var claims = new Claim[]
{
    new Claim("Name", content.Name),
    new Claim("Email", content.Email),
    new Claim("RoleId", content.RoleId.ToString()),
};

var ci = new ClaimsIdentity(claims, "password");
await HttpContext.Authentication.SignInAsync("Cookies", new ClaimsPrincipal(ci));

Now, if the user for example changes the email in the user profile, how can I change the e-mail value for the "Email" Claim? I have to SignOutAsync and SignInAsync again in order to update the cookie? The best solution is to save this into a classic session? There is a better solution? I'm totally wrong?

Any suggestions?

Answer

Alex picture Alex · Nov 26, 2016

Another option, instead of SignOutAsync and SignInAsync, is to use RefreshSignInAsync.

Example:

var user = await _userManager.FindByIdAsync(yourId);
await _signInManager.RefreshSignInAsync(user);

View the RefreshSignInAsync code in the SignInManager (netcore 3.1.8): https://github.com/dotnet/aspnetcore/blob/c75b3f7a2fb9fe21fd96c93c070fdfa88a2fbe97/src/Identity/Core/src/SignInManager.cs#L169