1

I would like to update the user profile. For this purpose,I wrote "Change Profile" method in web api.I have a problem with updating user data because "UpdateAsync" returns all the time null.

ChangeProfile

[HttpPost]
[Route("api/ChangeProfile")]
[ResponseType(typeof(AccountModel))]
public async Task<IHttpActionResult> ChangeProfile([FromBody]AccountModel model)
{
    var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
    var manager = new UserManager<ApplicationUser>(userStore);
    var user = await manager.FindByNameAsync(model.UserName);
    if (user != null)
    {
        var updateUser = new ApplicationUser()
        {   UserName = model.UserName,
            Email = model.Email,
            FirstName = model.FirstName,
            LastName = model.LastName,

        };

        IdentityResult result = await manager.UpdateAsync(updateUser);

        return Ok(result);
    }
    else
    {
        return NotFound();
    }
}

AccountModel:

  public class AccountModel
{
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string LoggedOn { get; set; }
    public string Roles { get; set; }
}

Any help or suggestion is welcome.

1
  • Hate to be captain obvious here, but your breakpoint is on a line of code that hasn't been executed yet. Press F10 to go to the next line (line 88) then see what the result object reads. If it doesn't even get to that, put your code in a try/catch. Commented Feb 27, 2019 at 16:03

1 Answer 1

2

Firstly you need go one more step on your debugging.

In addition, instead of creating a new AplicationUser, you must update the fields of the user that was retrieved from the database.

 if (user != null)
 {        
    user.UserName = model.UserName,
    user.Email = model.Email,
    user.FirstName = model.FirstName,
    user.LastName = model.LastName,
 };

 IdentityResult result = await manager.UpdateAsync(updateUser);
 return Ok(result);
 }

Regards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.