C# .NET Core Identity Update ApplicationUser

C#.NET Core

Please contribute by voting. Thanks!

So you need to update your ApplicationUser (or perhaps IdentityUser if you are using the default model i .NET Core Identity)? This is how you do it:

1) Inject UserMamager into your Page Model class:

    public class SettingsModel : PageModel
        private readonly UserManager<ApplicationUser> _userManager;

        public SettingsModel(
            UserManager<ApplicationUser> userManager
            _userManager = userManager;

    //Rest of the class...


2) Update the user's values in the OnPost-method:

        public async Task<IActionResult> OnPost(int currentUserId)
            if (!ModelState.IsValid)
                return Page();

            var user = await _userManager.FindByIdAsync(currentUserId);

            user.FirstName = Input.FirstName;
            user.LastName = Input.LastName;

            await _userManager.UpdateAsync(user);

            return Page();

Article created: Feb 14 '20. Edited Feb 14 '20.

Your comment

You need to sign up / log in to comment this article


Created by Peter Wright [8] Feb 14 '20

Share article

Do you know about


Write an article