我是MVC5 / ASP.NET身份的新手,我发现了一个让我感到困惑的问题。
我正在为我的ASP.NET MVC5应用程序编写一个小表单,它允许管理员用户(Admins角色的成员)查看已注册该站点的用户并编辑其详细信息并为其分配角色用户。提交表单时,如果已为用户分配了角色,则会调用UserManager.AddToRole方法。
之后我注意到,一旦完成,该用户就无法登录该应用程序。在数据库中查看,当调用AddToRole时,PasswordHash字段将设置为null。这是正常的,如果不是我怎么解决这个问题?
供参考,我的相关代码如下
控制器
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public ActionResult Details(EditUserViewModel model)
{
model.Save();
return RedirectToAction("List", "Account");
}
相关视图模型
public class EditUserViewModel
{
public string UserId { get; set; }
public string UserName { get; set; }
[Required]
[StringLength(255)]
[Display(Name = "Email address")]
public string Email { get; set; }
[StringLength(255)]
[Display(Name = "First name")]
public string FirstName { get; set; }
[StringLength(255)]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[StringLength(255)]
[Display(Name = "Mobile Number")]
public string MobileNumber { get; set; }
public IList<EditUserRolesViewModel> UserRoles { get; set; }
public void Save()
{
using (ApplicationDbContext context = new ApplicationDbContext())
{
ApplicationUser user = new ApplicationUser()
{
Id = this.UserId,
UserName = this.UserName,
Email = this.Email,
FirstName = this.FirstName,
LastName = this.LastName,
MobileNumber = this.MobileNumber
};
context.Users.Attach(user);
context.Entry(user).Property(x => x.Email).IsModified = true;
context.Entry(user).Property(x => x.FirstName).IsModified = true;
context.Entry(user).Property(x => x.LastName).IsModified = true;
context.Entry(user).Property(x => x.MobileNumber).IsModified = true;
context.SaveChanges();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
foreach (var row in this.UserRoles)
{
if (row.RowChanged)
{
if (row.RoleAssigned)
{
UserManager.AddToRole(this.UserId, row.RoleName);
}
else
{
UserManager.RemoveFromRole(this.UserId, row.RoleName);
}
}
}
}
}
}
public class EditUserRolesViewModel
{
public string RoleId { get; set; }
[Display(Name = "Role name")]
public string RoleName { get; set; }
[Display(Name = "Assigned")]
public bool RoleAssigned { get; set; }
public bool RowChanged { get; set; }
}
答案 0 :(得分:3)
正如我从您的代码中看到的那样,您已将部分初始化的对象user
附加到context.Users
。因此,当UserManager
获得控制权时AddToRole
,它会尝试更新数据库。并且您在当前用户行中有很多空字段或空字段。
您可以修复以下任何操作(两者都有帮助):
而不是
ApplicationUser user = new ApplicationUser()
使用
user = UserManager.FindById(UserId)
在从viewmodel分配值后,EntityFramework将处理已修改的字段。
在处理角色时使用其他上下文
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));