MVC5 ApplicationUser自定义属性

teh*_*ner 5 asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

我试图掌握ASP.NET MVC 5中引入的新成员系统,我遇到了一个小问题,我相信你能帮助我.

我将基于本教程并向ApplicationUser引入自定义属性,例如Name,Surname,DOB等.

但是,我试图更新当前登录的用户,而不是创建用户.我正在查看当前用于更改密码的控制器方法.

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);
    }
Run Code Online (Sandbox Code Playgroud)

例如,我将如何继续更新ApplicationUser的Surname?我需要调用DbContext还是?

我希望我的问题很明确.

jd4*_*d4u 6

探索IdentityManager.Store.UserManagementIdentityManager.Store.Users.

ApplicationUser cUser = (ApplicationUser) await IdentityManager.Store.Users.FindByNameAsync(HttpContext.User.Identity.Name, new System.Threading.CancellationToken());
cUser.Surname = "New Something";
IdentityResult result1 = await IdentityManager.Store.SaveChangesAsync();
Run Code Online (Sandbox Code Playgroud)

以上代码仅供参考.基本上你需要探索的Store属性IdentityManage.