在 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
class,我需要在调用更新方法之前将 Student
映射到 ApplicationUser
。另一方面,当使用我也用于创建新 Student 的方法时,当我创建新实例而不是更新时,由于并发性而出现错误。由于我厌倦了使用 AutoMapper
解决问题,我需要一个稳定的修复程序来解决没有 AutoMapper
的问题。你能告诉我如何解决这个问题吗?我将 StudentViewModel
传递给 Controller 中的 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
class 继承(因此 是) 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)
我在 MVC 5
项目中使用 ASP.NET Identity 2
,我想使用 UserManager.Update()
方法更新 Student
数据。但是,由于我继承自 ApplicationUser
class,我需要在调用更新方法之前将 Student
映射到 ApplicationUser
。另一方面,当使用我也用于创建新 Student 的方法时,当我创建新实例而不是更新时,由于并发性而出现错误。由于我厌倦了使用 AutoMapper
解决问题,我需要一个稳定的修复程序来解决没有 AutoMapper
的问题。你能告诉我如何解决这个问题吗?我将 StudentViewModel
传递给 Controller 中的 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
class 继承(因此 是) 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)