3

UserManager in ASPNET Identity contains functions like AddPasswordAsync and SetPhoneNumberAsync.

I've added properties for FirstName and LastName who's values weren't set when I registered the user so I want to add screens to Manage these values (setting and changing them).

How can I extend UserManager to add a function like AddNamesAsync that will do this?

Such a function becomes necessary when we evaluate the ManageController actions as below:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SetPassword(SetPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
        if (result.Succeeded)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
            if (user != null)
            {
                await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            }
            return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

Along the same logic, I'd like to add a function to the controller as follows:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> SetNames(SetUserFullNamesViewModel model)
{
    if (ModelState.IsValid)
    {
        var result = await UserManager.AddNamesAsync(User.Identity.GetUserId(), model.FirstName, model.LastName);
        if (result.Succeeded)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

            return RedirectToAction("Index", new { Message = ManageMessageId.SetNamesSuccess });
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

How can I add a function to UserManager or extend UserManager so that I can add the function that'll allow me to update add and update custom property values on the user?

adiga
  • 34,372
  • 9
  • 61
  • 83
Ortund
  • 8,095
  • 18
  • 71
  • 139
  • 1
    UserManager is meant to be inherited. Create your class, inherit UserManager, add whatever functions you need. – trailmax Jan 30 '17 at 10:31

1 Answers1

3

First add custom columns to AppliicationUser class

and then you can update custom columns like this

[HttpPost]
public async Task<ActionResult> SetNames(SetUserFullNamesViewModel model)
{
    ApplicationUser usermodel = UserManager.FindById(user.Id);
    usermodel.Name = model.Name;
    usermodel.Surname = model.Surname;


    IdentityResult result = await UserManager.UpdateAsync(usermodel);
    if (result.Succeeded)
    {
        return RedirectToAction("ListUser", "User");
    }

    return View();
}
kgzdev
  • 2,770
  • 2
  • 18
  • 35
  • There are a lot of methods that I don't know even exist on the .net framework but I guess practice make experience. Thank you – Fortune Aug 08 '17 at 09:43