public async Task <HidrogenianVM> InsertNewHidrogenian(RegistrationVM registration) { _logger.LogInformation("HidrogenianService.InserNewHidrogenian - Service starts."); var dbHidrogenian = new Hidrogenian { Email = registration.Email, UserName = registration.UserName, PasswordHash = registration.Password, PasswordSalt = registration.PasswordConfirm }; _dbContext.Hidrogenian.Add(dbHidrogenian); try { await _dbContext.SaveChangesAsync(); } catch (Exception e) { _logger.LogError("HidrogenianService.InserNewHidrogenian - Error: " + e); return(null); } HidrogenianVM hidrogenian = dbHidrogenian; hidrogenian.FamilyName = registration.FamilyName; hidrogenian.GivenName = registration.GivenName; return(hidrogenian); }
public async Task <bool> SetAccountConfirmationToken(HidrogenianVM hidrogenian) { _logger.LogInformation("HidrogenianService.SetAccountConfirmationToken - Service starts."); var dbHidrogenian = await _dbContext.Hidrogenian.FindAsync(hidrogenian.Id); dbHidrogenian.RecoveryToken = hidrogenian.Token; dbHidrogenian.TokenSetOn = DateTime.UtcNow; _dbContext.Update(dbHidrogenian); try { await _dbContext.SaveChangesAsync(); } catch (Exception e) { _logger.LogError("HidrogenianService.SetAccountConfirmationToken - Error: " + e); return(false); } return(true); }
public async Task <JsonResult> UpdateAccountIdentity(AccountIdentityVM identity) { _logger.LogInformation("AccountController.UpdateAccountIdentity - hidrogenianId=" + identity.Id); var validation = await _reCaptchaService.IsHumanRegistration(identity.CaptchaToken); if (!validation.Result) { return(new JsonResult(validation)); } var verification = VerifyIdentityData(identity); if (verification.Count != 0) { var messages = identity.GenerateErrorMessages(verification); return(new JsonResult(new { Result = RESULTS.FAILED, Message = messages })); } var(key, value) = await _accountService.UpdateIdentityForHidrogenian(identity); if (!key) { return(new JsonResult(new { Result = RESULTS.FAILED, Message = "Account not found with the given data. Please try again." })); } if (value == null) { return(new JsonResult(new { Result = RESULTS.FAILED, Message = "An error occurred while attempting to update your account. Please try again." })); } var oldIdentity = value.Value.Key; var newIdentity = value.Value.Value; var hidrogenian = new HidrogenianVM { Id = newIdentity.Id, Token = _authService.GenerateRandomToken() }; if (!newIdentity.EmailConfirmed) { if (await _userService.SetAccountConfirmationToken(hidrogenian)) { var profile = await _profileService.GetPublicProfileFor(newIdentity.Id); string emailTemplate; using (var reader = File.OpenText(PROJECT_FOLDER + @"HtmlTemplates/EmailChanged.html")) { emailTemplate = await reader.ReadToEndAsync(); }; emailTemplate = emailTemplate.Replace("[HidrogenianName]", profile.FullName); emailTemplate = emailTemplate.Replace("[HidrogenianEmail]", newIdentity.Email); emailTemplate = emailTemplate.Replace("[CONFIRM-TOKEN]", hidrogenian.Token); var emailChangedEmail = new EmailParamVM { ReceiverName = profile.FullName, ReceiverAddress = hidrogenian.Email, Subject = "Hidrogen - Confirm your email", Body = emailTemplate }; if (await _emailService.SendEmail(emailChangedEmail)) { await _accountService.ReverseIdentityChanges(oldIdentity); return(new JsonResult(new { Result = RESULTS.INTERRUPTED, Message = "The confirmation email was failed to send. Your changes was not applied. Please try again." })); } } } if (!string.IsNullOrEmpty(newIdentity.PhoneNumber) && !newIdentity.PhoneConfirmed) { //Send SMS to confirm phone number } return(new JsonResult(new { Result = RESULTS.SUCCESS, Message = newIdentity })); }