public async Task <IActionResult> SubmitUpdateAccount(GoblinIdentityUpdateIdentityModel model, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { ViewBag.WarningMessage = Messages.InvalidData; return(View("Account", model)); } try { await GoblinIdentityHelper.UpdateIdentityAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, model, cancellationToken).ConfigureAwait(true); ViewBag.SuccessMessage = "Account Updated Successfully."; if (!string.IsNullOrWhiteSpace(model.NewPassword)) { return(View("~/Views/Auth/Login.cshtml", new LoginModel { Continue = Url.AbsoluteAction("Account", "Portal") })); } } catch (GoblinException e) { ViewBag.ErrorMessage = e.ErrorModel.Message; } catch (Exception e) { ViewBag.ErrorMessage = e.Message; } return(View("Account", model)); }
public IActionResult Account() { var updateIdentityModel = new GoblinIdentityUpdateIdentityModel { NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName, NewEmail = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email }; return(View(updateIdentityModel)); }
public async Task <IActionResult> VerifyEmail(CancellationToken cancellationToken = default) { var updateIdentityModel = new GoblinIdentityUpdateIdentityModel { NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName, NewEmail = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email }; if (LoggedInUser <GoblinIdentityUserModel> .Current.Data.EmailConfirmedTime.HasValue) { ViewBag.ErrorMessage = "Your email already verified!"; return(View("Account", updateIdentityModel)); } try { var emailConfirmationModel = await GoblinIdentityHelper.RequestConfirmEmailAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, cancellationToken).ConfigureAwait(true); // Send Email var confirmEmailMessage = $"Your verify email code is {emailConfirmationModel.EmailConfirmToken}."; if (emailConfirmationModel.EmailConfirmTokenExpireTime.HasValue) { confirmEmailMessage += $"<br />Code will expire at {emailConfirmationModel.EmailConfirmTokenExpireTime.Value.ToString("f")}"; } var newEmailModel = new GoblinNotificationNewEmailModel { ToEmails = new List <string> { LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email }, Subject = $"{SystemSetting.Current.ApplicationName} | Verify Your Email", HtmlBody = confirmEmailMessage }; await GoblinNotificationHelper.SendAsync(newEmailModel, cancellationToken); ViewBag.WarningMessage = "Please check your email inbox to get the Verify Code."; return(View()); } catch (GoblinException e) { ViewBag.ErrorMessage = e.ErrorModel.Message; } catch (Exception e) { ViewBag.ErrorMessage = e.Message; } return(View("Account", updateIdentityModel)); }
public async Task <IActionResult> SubmitVerifyEmail(GoblinIdentityConfirmEmailModel model, CancellationToken cancellationToken = default) { if (!ModelState.IsValid) { ViewBag.WarningMessage = Messages.InvalidData; return(View("VerifyEmail", model)); } try { model.LoggedInUserId = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id; await GoblinIdentityHelper.ConfirmEmailAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, model, cancellationToken).ConfigureAwait(true); ViewBag.SuccessMessage = "Your email is verified."; LoggedInUser <GoblinIdentityUserModel> .Current.Data.EmailConfirmedTime = GoblinDateTimeHelper.SystemTimeNow; } catch (GoblinException e) { ViewBag.ErrorMessage = e.ErrorModel.Message; } catch (Exception e) { ViewBag.ErrorMessage = e.Message; } var updateIdentityModel = new GoblinIdentityUpdateIdentityModel { NewUserName = LoggedInUser <GoblinIdentityUserModel> .Current.Data.UserName, NewEmail = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Email }; return(View("Account", updateIdentityModel)); }
public async Task <IActionResult> UpdateIdentity([FromRoute] long id, [FromBody] GoblinIdentityUpdateIdentityModel model, CancellationToken cancellationToken = default) { var emailConfirmationModel = await _userService.UpdateIdentityAsync(id, model, cancellationToken); return(Ok(emailConfirmationModel)); }
public static async Task <GoblinIdentityEmailConfirmationModel> UpdateIdentityAsync(long id, GoblinIdentityUpdateIdentityModel model, CancellationToken cancellationToken = default) { ValidationHelper.Validate <GoblinIdentityUpdateIdentityModelValidator, GoblinIdentityUpdateIdentityModel>(model); try { var endpoint = GetRequest(model.LoggedInUserId).AppendPathSegment(GoblinIdentityEndpoints.UpdateIdentity.Replace("{id}", id.ToString())); var emailConfirmModel = await endpoint .PutJsonAsync(model, cancellationToken : cancellationToken) .ReceiveJson <GoblinIdentityEmailConfirmationModel>() .ConfigureAwait(true); return(emailConfirmModel); } catch (FlurlHttpException ex) { await FlurlHttpExceptionHelper.HandleErrorAsync(ex).ConfigureAwait(true); return(null); } }
public async Task <GoblinIdentityEmailConfirmationModel> UpdateIdentityAsync(long id, GoblinIdentityUpdateIdentityModel model, CancellationToken cancellationToken = default) { model.NewEmail = model.NewEmail?.Trim().ToLowerInvariant(); model.NewUserName = model.NewUserName?.Trim().ToLowerInvariant(); var userEntity = await _userRepo.Get(x => x.Id == id) .FirstOrDefaultAsync(cancellationToken) .ConfigureAwait(true); if (userEntity == null) { throw new GoblinException(nameof(GoblinIdentityErrorCode.UserNotFound), GoblinIdentityErrorCode.UserNotFound); } var currentPasswordHashWithOldSalt = PasswordHelper.HashPassword(model.CurrentPassword, userEntity.PasswordLastUpdatedTime); if (currentPasswordHashWithOldSalt != userEntity.PasswordHash) { throw new GoblinException(nameof(GoblinIdentityErrorCode.WrongPassword), GoblinIdentityErrorCode.WrongPassword); } var emailConfirmationModel = new GoblinIdentityEmailConfirmationModel { Id = userEntity.Id }; var changedProperties = new List <string>(); // Update Password if (!string.IsNullOrWhiteSpace(model.NewPassword)) { var newPasswordHashWithOldSalt = PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime); // If user have changed password, then update password and related information if (newPasswordHashWithOldSalt != userEntity.PasswordHash) { userEntity.PasswordLastUpdatedTime = userEntity.RevokeTokenGeneratedBeforeTime = GoblinDateTimeHelper.SystemTimeNow; changedProperties.Add(nameof(userEntity.PasswordLastUpdatedTime)); changedProperties.Add(nameof(userEntity.RevokeTokenGeneratedBeforeTime)); userEntity.PasswordHash = PasswordHelper.HashPassword(model.NewPassword, userEntity.PasswordLastUpdatedTime); changedProperties.Add(nameof(userEntity.PasswordHash)); } } // Update Email if (!string.IsNullOrWhiteSpace(model.NewEmail) && model.NewEmail != userEntity.Email) { CheckUniqueEmail(model.NewEmail); userEntity.EmailConfirmToken = StringHelper.Generate(6, false, false); changedProperties.Add(nameof(userEntity.EmailConfirmToken)); userEntity.EmailConfirmTokenExpireTime = GoblinDateTimeHelper.SystemTimeNow.Add(SystemSetting.Current.EmailConfirmTokenLifetime); changedProperties.Add(nameof(userEntity.EmailConfirmTokenExpireTime)); // Email Confirmation Token emailConfirmationModel.EmailConfirmToken = userEntity.EmailConfirmToken; emailConfirmationModel.EmailConfirmTokenExpireTime = userEntity.EmailConfirmTokenExpireTime; } // Update UserName if (!string.IsNullOrWhiteSpace(model.NewUserName)) { CheckUniqueUserName(model.NewUserName); userEntity.UserName = model.NewUserName; changedProperties.Add(nameof(userEntity.UserName)); } if (!changedProperties.Any()) { return(emailConfirmationModel); } _userRepo.Update(userEntity, changedProperties.ToArray()); await GoblinUnitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(true); return(emailConfirmationModel); }