In this post, we will discuss how to update claims in ASP.NET Core. Let's take an example: I have claim values such as Name, Email, UserId, Business_Name, and ProfilePicture. I need to update the claims when someone updates their profile picture or other profile information, so that I can show the updated user information in the user profile. How can I achieve this? lets discuess.

To update the claims when a user updates their profile picture or any other profile information, you can follow these steps:

  • Retrieve User Profile Information: When the user updates their profile, retrieve the updated profile information such as Name, Email, UserId, Business_Name, and ProfilePicture.
  • Update Claims: Use the retrieved profile information to update the corresponding claims for the user. This typically involves updating the existing claims or adding new claims with updated values.
  • Sign In User: After updating the claims, sign in the user again to update their authentication token with the new claims.
Here's an example of how you can achieve this in an ASP.NET Core controller action:


using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;

namespace TestProject.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ProfileController : ControllerBase
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public ProfileController(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpPost("updateProfile")]
        public async Task<IActionResult> UpdateProfile([FromBody] UserProfileUpdateModel profileUpdate)
        {
            // Retrieve the current user principal
            var userPrincipal = _httpContextAccessor.HttpContext.User as ClaimsPrincipal;

            if (userPrincipal != null)
            {
                // Update claims with new profile information
                ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(userPrincipal.FindFirst(ClaimTypes.Name));
                ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Name, profileUpdate.Name));

                ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(userPrincipal.FindFirst(ClaimTypes.Email));
                ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim(ClaimTypes.Email, profileUpdate.Email));

                ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(userPrincipal.FindFirst("UserId"));
                ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim("UserId", profileUpdate.UserId));

                ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(userPrincipal.FindFirst("Business_Name"));
                ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim("Business_Name", profileUpdate.BusinessName));

                ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(userPrincipal.FindFirst("ProfilePicture"));
                ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim("ProfilePicture", profileUpdate.ProfilePicture));

                // Update the user's identity
                await _httpContextAccessor.HttpContext.SignInAsync(userPrincipal);
            }

            return Ok("Profile updated successfully");
        }
    }

    public class UserProfileUpdateModel
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string UserId { get; set; }
        public string BusinessName { get; set; }
        public string ProfilePicture { get; set; }
    }
}

  • We have a ProfileController with an action UpdateProfile that handles updating user profile information.
  • The action takes a UserProfileUpdateModel object from the request body containing the updated profile information.
  • We retrieve the current user's principal and update the relevant claims with the new profile information.
  • Finally, we sign in the user again to update their authentication token with the new claims.

Ensure that the properties in UserProfileUpdateModel match the profile information you want to update, and adjust the implementation accordingly to fit your application's requirements.


To update claims values in ASP.NET Core, you typically work with the ClaimsPrincipal object. Here's a basic example of how you can update claim values: 
Here's a basic example of how you can update claim values:
using System.Security.Claims;

// Retrieve the current user principal
var userPrincipal = HttpContext.User as ClaimsPrincipal;

if (userPrincipal != null)
{
    // Find the claim you want to update
    var existingClaim = userPrincipal.FindFirst("ClaimType");

    if (existingClaim != null)
    {
        // Remove the existing claim
        ((ClaimsIdentity)userPrincipal.Identity).RemoveClaim(existingClaim);

        // Add the updated claim
        ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim("ClaimType", "NewClaimValue"));
    }
    else
    {
        // Add the claim if it doesn't exist
        ((ClaimsIdentity)userPrincipal.Identity).AddClaim(new Claim("ClaimType", "ClaimValue"));
    }

    // Update the user's identity
    await HttpContext.SignInAsync(userPrincipal);
}
Above example code assumes We're working within an ASP.NET Core controller action. Make sure you have the necessary using directives (System.Security.Claims), and ensure you're in a context where HttpContext is available, such as within a controller in a web application. 
 Replace "ClaimType" and "ClaimValue" with the type and value of the claim you want to update. You can also customize this code to fit your specific use case, such as fetching data from a database to update the claims dynamically.