Updating user by UserManager.Update() in ASP.NET Identity 2

后端 未结 3 1872
盖世英雄少女心
盖世英雄少女心 2021-02-05 06:25

I use ASP.NET Identity 2 in an MVC 5 project and I want to update Student data by using UserManager.Update() method. However,

相关标签:
3条回答
  • 2021-02-05 06:37

    This took me a little while to understand, but you simply need to use the UserManager. My task was to simply update the LoginViewModel with a new Date. I tried different methods, but the only thing that seemed to work was FindByEmailAsync, which is specific to my MODEL because I'm not using Id to sign in.

    Case SignInStatus.Success
                Dim user As ApplicationUser = Await UserManager.FindByEmailAsync(model.Email)
                user.LastActivityDate = model.LastActivityDate
    
                UserManager.Update(user)
    
                Return RedirectToLocal(returnUrl)
    
    0 讨论(0)
  • 2021-02-05 06:40

    There is no need to pass the student as ApplicationUser to the UserManager.Update() method (because Student class inherits (hence is) ApplicationUser).

    The problem with your code is that you are using new Student operator, thus creating a new student rather than updating the existing one.

    Change the code like this:

    // Get the existing student from the db
    var user = (Student)UserManager.FindById(model.Id);
    
    // Update it with the values from the view model
    user.Name = model.Name;
    user.Surname = model.Surname;
    user.UserName = model.UserName;
    user.Email = model.Email;
    user.PhoneNumber = model.PhoneNumber;
    user.Number = model.Number; //custom property
    user.PasswordHash = checkUser.PasswordHash;
    
    // Apply the changes if any to the db
    UserManager.Update(user);
    
    0 讨论(0)
  • 2021-02-05 06:46

    my answer on .netcore 1

    this work for my, I hope can help them

    var user = await _userManager.FindByIdAsync(applicationUser.Id);
                        user.ChargeID = applicationUser.ChargeID;
                        user.CenterID = applicationUser.CenterID;
                        user.Name  = applicationUser.Name;
    var result = await _userManager.UpdateAsync(user);
    
    0 讨论(0)
提交回复
热议问题