Custom MVC5 ApplicationUser Properties

I am trying to deal with the new membership system introduced in ASP.NET MVC 5, and I ran into a little problem, and I'm sure you can help me.

I build on this tutorial and introduced custom ApplicationUser properties like Name, Surname, DOB, etc.

However, instead of creating a user, I am trying to upgrade it now. I am considering the controller method that is currently used to change the 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 will I continue to update the last name of ApplicationUser? Do I need to call DbContext or?

Hope my question is clear.

+5
asp.net-mvc asp.net-mvc-5 asp.net-identity
Oct 13 '13 at 18:52
source share
3 answers

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

Only the code is an example. Basically you need to examine the Store IdentityManage property.

+6
Oct 14 '13 at 15:32
source share

When we used the Users object of our database context, we encountered other tracking errors. In our application we will search for 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 will save it using the UserManager in the controller:

 UserManager.Update(user); 

This is currently a working solution for us.

+2
Dec 14 '14 at 3:33
source share

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

 public class ApplicationUser : IdentityUser { public virtual Person Person { get; set; } 
+2
Jul 27 '15 at 19:53
source share



All Articles