MVC5 ApplicationUser custom properties

2019-02-07 11:00发布

I am trying to get to grips with the new Membership system introduced in ASP.NET MVC 5 and I've come across a small issue which I am pretty sure you will be able to help me with.

I am going based off this tutorial and have introduced custom properties to ApplicationUser such as Name, Surname, DOB, etc.

However, instead of creating the user, I am trying to update the currently logged in one. I am looking at the controller method which is currently used to change password.

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);
    }

How exactly would I go on about updating an ApplicationUser's Surname for example? Do I need to call the DbContext or?

I hope my question is clear.

3条回答
ゆ 、 Hurt°
2楼-- · 2019-02-07 11:09

Explore IdentityManager.Store.UserManagement and IdentityManager.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();

Above code is an example only. Basically you need to explore the Store property of IdentityManage.

查看更多
来,给爷笑一个
3楼-- · 2019-02-07 11:25

When we used the Users object of our database context we ran into other tracking errors. In our application, we would retrieve users as such

var user = UserManager.FindById(userId);

Edit the properties:

user.StorageName = "gooblygook";
//whatever other properties you would like to use

And then we would save it with the UserManager in the controller:

UserManager.Update(user);

This is currently a working solution for us.

查看更多
Fickle 薄情
4楼-- · 2019-02-07 11:35

Mark the Person object as virtual in your ApplicationUser definition. That worked for me.

public class ApplicationUser : IdentityUser
{
    public virtual Person Person { get; set; }
查看更多
登录 后发表回答