public void ConfirmEmail(ConfirmEmailModel confirmEmailModel) { if (confirmEmailModel.UserId == null || confirmEmailModel.UserId == null) { throw new BusinessException(OperationResultCode.Error, "Wrong Data"); } var id = new Guid(confirmEmailModel.UserId); var user = _userRepository.Get().FirstOrDefault(x => x.Id == id); if (user == null || user.EmailConfirmed) { throw new BusinessException(OperationResultCode.Error, "NotValidToken"); } var result = Sha256Encryption.Sha256HexHashString(user.Email); if (result == confirmEmailModel.Code) { user.EmailConfirmed = true; _userRepository.SaveChanges(); } else { throw new BusinessException(OperationResultCode.Error, "NotValidToken"); } }
public void EmailNotification(SignupModel model) { var isCreateUser = _userRepository.CreateUser(new User { UserName = model.Username, Email = model.Email, EmailConfirmed = false }, model.Password); if (!isCreateUser.Succeeded) { throw new BusinessException(OperationResultCode.InvalidUser, "Error"); } User user = _userRepository.Get().FirstOrDefault(x => x.Email == model.Email); string token = Sha256Encryption.Sha256HexHashString(model.Email); string uiUrl = _settingsService.UIUrlSettings.Url; if (user != null) { string url = String.Format("{2}/auth/confirm-email?UserId={0}&Code={1}", user.Id, token, uiUrl); _emailNotificationService.SendMail(model.Email, "Notification", $"For confirm email, follow the link: {url}"); } else { throw new BusinessException(OperationResultCode.InvalidUser, "Not found user with current email"); } }