I am working on a webapplication with .NET Core 2 Razor Pages and individual accounts authentication.
I extended my db with firstname since it's not implemented bystandard. Everything is working properly. However, I'd like to extend my /Account/Manage page to make sure users are able to change their own name.
The OnGetAsync works fine, but when my OnPostAsync is not working as it should be.
OnPostAsync
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
if (Input.Email != user.Email)
{
var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);
if (!setEmailResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
}
}
if (Input.PhoneNumber != user.PhoneNumber)
{
var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
if (!setPhoneResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
}
}
// not working yet
if (Input.FirstName != user.FirstName)
{
var setFirstNameResult = await MyManager.SetFirstNameAsync(user, Input.FirstName);
if (!setFirstNameResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting first name for user with ID '{user.Id}'.");
}
}
StatusMessage = "Your profile has been updated";
return RedirectToPage();
}
MyManager.cs
public class MyManager : UserManager<ApplicationUser>
{
public MyManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public static async Task<IdentityResult> SetFirstNameAsync(ApplicationUser user, string FirstName)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (FirstName == null)
{
throw new ArgumentNullException(nameof(FirstName));
}
user.FirstName = FirstName;
return IdentityResult.Success;
}
}
It fails when I hit the 'Save' button and tells me my profile has been succesfully updated, but it did not. It just kept the old first name value. What am I missing here?
ApplicationUser
object with my own custom added property ofpublic string FirstName { get; set; }
. I.e., the method retrieves the current ApplicationUser from the db – M. Douglas