public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new FpUser
                {
                    UserName  = Input.Email,
                    Email     = Input.Email,
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName
                };
                if (Input.Avatar != null)
                {
                    user.ImageName = Input.Avatar.FileName;
                    user.ImageData = await _fileService.ConvertFileToByteArrayAsync(Input.Avatar);
                }
                //else
                //{
                //    var image = _configuration.GetSection("AdminSettings")["ImageName"];
                //    user.ImageData = await _fileService.AssignDefaultAvatarAsync(image);
                //    user.ImageName = image;
                //}
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded && string.IsNullOrEmpty(Input.Code))
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        TempData["PleaseConfirm"] = $"Your email {Input.Email} has been registered. Please check your email and confirm before logging in.";

                        return(RedirectToPage("/Account/Login", new { area = "Identity" }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                else if (result.Succeeded && !string.IsNullOrEmpty(Input.Code))
                {
                    user.EmailConfirmed = true;
                    var invitation = _context.Invitations.FirstOrDefault(i => i.Code.ToString() == Input.Code);
                    invitation.Accepted = true;
                    var household = _context.HouseHolds.FirstOrDefault(hh => hh.Id == invitation.HouseHoldId);
                    household.Members.Add(user);
                    await _userManager.AddToRoleAsync(user, Roles.Member.ToString());

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(RedirectToAction("Details", "HouseHolds", new { id = household.Id }));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (User.IsInRole(nameof(Roles.Demo)))
            {
                TempData["Alert"] = "That action can not be done by demo users";
                return(RedirectToAction("Index", "Home"));
            }
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            if (!ModelState.IsValid)
            {
                await LoadAsync(user);

                return(Page());
            }


            if (Input.FirstName != user.FirstName)
            {
                user.FirstName = Input.FirstName;
                var update = await _userManager.UpdateAsync(user);

                if (!update.Succeeded)
                {
                    StatusMessage = "Error when trying update first name.";
                    return(RedirectToPage());
                }
            }

            if (Input.LastName != user.LastName)
            {
                user.LastName = Input.LastName;
                var update = await _userManager.UpdateAsync(user);

                if (!update.Succeeded)
                {
                    StatusMessage = "Error when trying update last name.";
                    return(RedirectToPage());
                }
            }

            if (Input.Email != user.Email)
            {
                user.Email    = Input.Email;
                user.UserName = Input.Email;
                var update = await _userManager.UpdateAsync(user);

                if (!update.Succeeded)
                {
                    StatusMessage = "Error when trying update email.";
                    return(RedirectToPage());
                }
            }

            if (Input.Avatar != null)
            {
                user.ImageData = await _fileService.ConvertFileToByteArrayAsync(Input.Avatar);

                user.ImageName = Input.Avatar.FileName;
                var update = await _userManager.UpdateAsync(user);

                if (!update.Succeeded)
                {
                    StatusMessage = "Error when trying update avatar.";
                    return(RedirectToPage());
                }
            }

            if (Input.Password != null && Input.OldPassword != null)
            {
                var changePasswordResult = await _userManager.ChangePasswordAsync(user, Input.OldPassword, Input.Password);

                if (!changePasswordResult.Succeeded)
                {
                    foreach (var error in changePasswordResult.Errors)
                    {
                        ModelState.AddModelError(string.Empty, error.Description);
                    }
                    return(Page());
                }
            }


            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Your profile has been updated";
            return(RedirectToPage());
        }