public async Task <IActionResult> OnPostAsync(IFormFile file, string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // Implementar o upload da imagem de perfil // Posteriomente vamos utilizar o Patterns IUnitOfWork; if (!(file == null || string.IsNullOrEmpty(file.FileName))) { _unitOfUpload.UploadImage(file); } // Concretagem + Alvenaria // Arquiteto > Parede >> Mestre de Obras >> Pedreiros >> Tijolos >> Fábrica >> Barro/Natureza var user = new ApplicationUser { UserName = Input.Email, Email = Input.Email, Apelido = Input.Apelido, NomeCompleto = Input.NomeCompleto, DataNascimento = Input.DataNascimento, ImgProfilePath = file != null ? file.FileName : "" }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { userId = user.Id, code = code }, 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>."); await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } 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(IFormFile file) { if (!ModelState.IsValid) { return(Page()); } var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var email = await _userManager.GetEmailAsync(user); if (Input.Email != email) { var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email); if (!setEmailResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting email for user with ID '{userId}'."); } } var phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'."); } } if (file != null) { _unitOfUpload.UploadImage(file); user.ImgProfilePath = file.FileName; } if (Input.Apelido != user.Apelido) { user.Apelido = Input.Apelido; } if (Input.NomeCompleto != user.NomeCompleto) { user.NomeCompleto = Input.NomeCompleto; } if (Input.DataNascimento != user.DataNascimento) { user.DataNascimento = Input.DataNascimento; } await _userManager.UpdateAsync(user); await _signInManager.RefreshSignInAsync(user); StatusMessage = "Perfil atualizado com sucesso!"; return(RedirectToPage()); }