MVC5 Identity UserManager.Update(user) not working
本文关键字:not working user Identity UserManager Update MVC5 | 更新日期: 2023-09-27 18:36:52
这是我第一次在 MVC5 中使用代码优先进行用户管理。我可以更改密码,但无法更新用户注册。我已经梳理了互联网,从字面上看了这个网站上与UserManager.Update(user)不起作用相关的每个问题。
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace _100..Models
{
public class ApplicationUser : IdentityUser
{
public virtual PersonalInfo PersonalInfo { get; set; }
public virtual BillingInfo BillingInfo { get; set; }
public virtual DeliveryInfo DeliveryInfo { get; set; }
public Chapters Chapter { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class PersonalInfo
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class BillingInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class DeliveryInfo
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false){}
public DbSet<PersonalInfo> PersonalInfo { get; set; }
public DbSet<BillingInfo> BillingInfo { get; set; }
public DbSet<DeliveryInfo> DeliveryInfo { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
这是我的行动
[HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser user)
{
var result = await UserManager.UpdateAsync(user);
return RedirectToAction("Index", "Home");
}
我已经确认用户对象在点击操作时具有更新的数据,并且更新方法返回成功,但它实际上并没有更新数据库。
此修复有几个层。首先,当我执行UserManager.Update时,它是针对我从updateRegistration视图中收到的用户对象。EF 认为这是一个新对象,并引发内部错误"用户名已存在",并且失败但报告成功。所以我必须创建一个 dbContext 并将实体状态更新为已修改。但我了解到,我还必须从更新的用户 ID 创建一个用户对象来设置用户名,否则更新将失败。我发现我必须更新密码哈希,否则它将为空。然后我发现我还必须更新安全印章,否则登录过程会引发错误。我的行动如下。
[HttpPost]
public async Task<ActionResult> UpdateRegisteration(ApplicationUser UpdatedUser)
{
var SavedUser = await UserManager.FindByIdAsync(UpdatedUser.Id);
try
{
UpdatedUser.SecurityStamp = SavedUser.SecurityStamp;
UpdatedUser.PasswordHash = SavedUser.PasswordHash;
UpdatedUser.UserName = SavedUser.UserName;
UpdatedUser.Id = SavedUser.Id;
UpdatedUser.PersonalInfo.ID = SavedUser.PersonalInfo.ID;
UpdatedUser.BillingInfo.ID = SavedUser.BillingInfo.ID;
UpdatedUser.DeliveryInfo.ID = SavedUser.DeliveryInfo.ID;
ApplicationDbContext db = new ApplicationDbContext();
db.Entry(UpdatedUser).State = EntityState.Modified;
db.Entry(UpdatedUser.PersonalInfo).State = EntityState.Modified;
db.Entry(UpdatedUser.BillingInfo).State = EntityState.Modified;
db.Entry(UpdatedUser.DeliveryInfo).State = EntityState.Modified;
await db.SaveChangesAsync();
// var result = await UserManager.UpdateAsync(SavedUser);
return RedirectToAction("Index", "Home");
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
}