How do I change the FirstName property in a SQL database in .NET Core?

Viewed 74

I have successfully authenticated my app, but the only thing that I can't seem to do is change the user properties that I have made.

public async Task<IActionResult> OnPostAsync()
{
    var user = await _userManager.GetUserAsync(User); 

    if (user == null)
    {
        return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
    }

    if (!ModelState.IsValid)
    {
        LoadAsync(user);
        return Page(); 
    }

    var phonenumber = await _userManager.GetPhoneNumberAsync(user);
        
    if (Input.PhoneNumber != phonenumber)
    {
        var setPhoneNumber = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
    }

    user.FirstName = Input.FirstName; 

    return RedirectToPage(); 
}

If anybody knows how to change the FirstName property, thank you.

1 Answers

I have successfully authenticated my app, but the only thing that I can't seem to do is change the user properties that I have made.

You can try to call UserManager<TUser>.UpdateAsync(TUser) method to update the specified user, like below.

//set FirstName of current user
user.FirstName = Input.FirstName;

//update the specified user
await _userManager.UpdateAsync(user);
Related