无法更新mvc5中的身份用户

时间:2016-08-31 01:08:14

标签: c# asp.net-mvc-5 asp.net-identity

我正在尝试使用ASP.NET Identity锁定MVC 5中的用户。我通过以下方式遵循其他SO帖子的建议(所有基本上都说同样的事情):

var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new ApplicationUserManager(store);
var user = new ApplicationUser() {
    Id = form["AspNetUserID"],
    LockoutEnabled = true,
    LockoutEndDateUtc = DateTime.UtcNow.AddYears(100)
};
IdentityResult result = await manager.UpdateAsync(user);
var context = store.Context;
context.SaveChanges();

return RedirectToAction(actionName: "Index", controllerName: "SiteUsersAdmin");

我已经逐步完成了上面的代码,并在返回行显示了一个断点,以找出错误,并且我不断获得Name cannot be null or empty{Email/UserName} is already selected

我将该帐户锁定,甚至将来解锁?

1 个答案:

答案 0 :(得分:1)

这是一些伪代码......

var user = UserManager.FindById(form["AspNetUserID"]);
user.LockoutEnabled = true;
user.LockoutEndDateUtc = DateTime.UtcNow.AddYears(100)};

var result = await manager.UpdateAsync(user);

if (result.Succeeded)
{
    //success
}
else
{
    //something went wrong
}