Update a user account

Introduction


In order to update a user account in ASP.NET Identity, we use the Update method.

Update


The Update method updates the user passed as a parameter of the method and returns the result of the operation as an IdentityResult object.

IdentityResult result = UserManager.Update(UserToEdit);

The Update method is called by the UserManager which is responsible for performing the user-related operations.

UserManager


We use the GetUserManager method to get the ApplicationUserManager from the OWIN context.

ApplicationUserManager UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

We use the ApplicationUserManager instead of the UserManager in order to use the default validation logic for usernames and passwords configured in the IdentityConfig.cs file.

Namespaces


In order to use the GetUserManager method, the Update method and the IdentityResult object, you have to include the following namespaces :

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;

Example


[HttpPost]
public ActionResult UpdateUser(EditUserViewModel model)
{
    if (ModelState.IsValid)
    {
        ApplicationUserManager UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

        ApplicationUser UserToEdit = UserManager.FindById(model.UserId);

        if (UserToEdit.UserName != model.UserName)
            UserToEdit.UserName = model.UserName;

        if (UserToEdit.FirstName != model.FirstName)
            UserToEdit.FirstName = model.FirstName;

        if (UserToEdit.LastName != model.LastName)
            UserToEdit.LastName = model.LastName;

        if (UserToEdit.Email != model.Email)
            UserToEdit.Email = model.Email;

        if (UserToEdit.PhoneNumber != model.PhoneNumber)
            UserToEdit.PhoneNumber = model.PhoneNumber;

        IdentityResult result = UserManager.Update(UserToEdit);
        if (result.Succeeded)
        {
            return RedirectToAction("Index", "Home");
        }

        foreach (string error in result.Errors)
            ModelState.AddModelError("", error);
    }

    return View(model);
}

In the above example, we get the information of the user to update via an HTTP Post request. We get the user manager from the OWIN context by using the GetUserManager method, we use the FindById method to find the user to update by its id and then we use the Update method in order to update this user. We access the Succeeded property of the IdentityResult object to check if the opration was successful. If it is not the case, we loop through the list of Errors and we add them to the ModelState by using the AddModelError method.

The EditUserViewModel object is the ViewModel that we are using in order to render and validate the UpdateUser view.

public class EditUserViewModel
{
    public string UserId { get; set; }

    [Display(Name = "Username")]
    [Required]
    public string UserName { get; set; }

    [Display(Name = "First name")]
    public string FirstName { get; set; }

    [Display(Name = "Last name")]
    public string LastName { get; set; }

    [Display(Name = "Email")]
    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Display(Name = "Phone number")]
    [Phone]
    public string PhoneNumber { get; set; }
}

Find the user to update


In addition to the FindById method, we can also use the FindByName method or the FindByEmail method to find the user to update by its username or by its email address.