簡體   English   中英

通過 ASP.NET Identity 2 中的 UserManager.Update() 更新用戶

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

我在MVC 5項目中使用ASP.NET Identity 2 ,我想使用UserManager.Update()方法更新Student數據。 但是,當我從ApplicationUser類繼承時,我需要在調用 update 方法之前將Student映射到ApplicationUser 另一方面,當使用我也用於創建新 Student 的方法時,由於我創建新實例而不是更新實例時發生並發性錯誤。 由於我厭倦了使用AutoMapper解決問題,因此我需要一個穩定的修復程序來解決沒有AutoMapper的問題。 你能澄清我如何解決這個問題嗎? 我將StudentViewModel傳遞給StudentViewModel中的 Update 方法,然后我需要將它映射到 Student,然后將它們作為ApplicationUser傳遞給UserManager.Update()方法。 另一方面,我想知道是否應該在 Controller 階段檢索並發送密碼,而不是出於安全考慮傳遞給 View? 你能否也告訴我這個問題(在用戶更新期間我不更新密碼,我必須將用戶的密碼保存在數據庫中)。 任何幫助將不勝感激。

實體類:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin,
                                     ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public string Name { get; set; }
    public string Surname { get; set; } 
    //code omitted for brevity
}

public class Student: ApplicationUser
{     
    public int? Number { get; set; }
}


控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Update([Bind(Exclude = null)] StudentViewModel model)
{
    if (ModelState.IsValid)
    {
        ApplicationUser user = UserManager.FindById(model.Id);

        user = new Student
        {
            Name = model.Name,
            Surname = model.Surname,
            UserName = model.UserName,
            Email = model.Email,
            PhoneNumber = model.PhoneNumber,
            Number = model.Number, //custom property
            PasswordHash = checkUser.PasswordHash
        };

        UserManager.Update(user);
    }
}

無需將student作為ApplicationUser傳遞給UserManager.Update()方法(因為Student類繼承(因此ApplicationUser )。

您的代碼的問題在於您使用的是new Student運算符,因此創建了一個新學生而不是更新現有學生。

像這樣更改代碼:

// 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);

我在 .netcore 1 上的回答

這項工作適合我,我希望可以幫助他們

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);

這花了我一點時間來理解,但您只需要使用 UserManager。 我的任務是簡單地使用新日期更新 LoginViewModel。 我嘗試了不同的方法,但似乎唯一有效的是 FindByEmailAsync,它特定於我的模型,因為我沒有使用 Id 登錄。

Case SignInStatus.Success
            Dim user As ApplicationUser = Await UserManager.FindByEmailAsync(model.Email)
            user.LastActivityDate = model.LastActivityDate

            UserManager.Update(user)

            Return RedirectToLocal(returnUrl)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM