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?