我試圖去掌握在ASP.NET MVC 5中引入的新的成員系統,我遇到了一個小問題,我很肯定你能幫助我。MVC5 ApplicationUser自定義屬性
我會根據關this tutorial,並推出諸如姓名,出生日期等
然而,而不是創建用戶,我試圖更新一個當前登錄的自定義屬性ApplicationUser。我正在查看當前用於更改密碼的控制器方法。
public async Task<ActionResult> Manage(ManageUserViewModel model)
{
string userId = User.Identity.GetUserId();
bool hasLocalLogin = await IdentityManager.Logins.HasLocalLoginAsync(userId);
ViewBag.HasLocalPassword = hasLocalLogin;
ViewBag.ReturnUrl = Url.Action("Manage");
if (hasLocalLogin)
{
if (ModelState.IsValid)
{
IdentityResult result = await IdentityManager.Passwords.ChangePasswordAsync(User.Identity.GetUserName(), model.OldPassword, model.NewPassword);
if (result.Success)
{
return RedirectToAction("Manage", new { Message = "Your password has been changed." });
}
else
{
AddErrors(result);
}
}
}
else
{
// User does not have a local password so remove any validation errors caused by a missing OldPassword field
ModelState state = ModelState["OldPassword"];
if (state != null)
{
state.Errors.Clear();
}
if (ModelState.IsValid)
{
// Create the local login info and link it to the user
IdentityResult result = await IdentityManager.Logins.AddLocalLoginAsync(userId, User.Identity.GetUserName(), model.NewPassword);
if (result.Success)
{
return RedirectToAction("Manage", new { Message = "Your password has been set." });
}
else
{
AddErrors(result);
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
我到底會如何去更新ApplicationUser的姓氏?我是否需要調用DbContext或?
我希望我的問題很清楚。
+1您可以從System.Threading中使用CancellationToken.None,而不是創建一個BTW。 – Stimul8d
真棒東西傢伙!謝謝 – teh0wner