public async Task SendConfirmationMail(int userId) { var user = _context.Users.FirstOrDefault(u => u.Id == userId); if (user == null) { _logger.LogWarning(LoggingEvents.HaveException, $"not found user"); throw new NotFoundException("user", userId); } if (user.EmailConfirmed) { _logger.LogWarning(LoggingEvents.HaveException, $"email confirmed exception"); throw new EmailConfirmedException(); } string token = GenerateSymbols.GenerateRandomSymbols(); var verificationToken = new VerificationToken() { Token = token, UserId = userId }; _context.VerificationTokens.Add(verificationToken); await _context.SaveChangesAsync(); await _emailService.SendEmailVerificationMail(user.Email, token); }
public async Task RecoverPassword(string email) { var user = _context.Users.FirstOrDefault(u => u.Email == email); if (user == null) { _logger.LogWarning(LoggingEvents.HaveException, $"not user with such email"); throw new NotFoundException("User with such email was"); } var salt = SecurityHelper.GetRandomBytes(); var password = GenerateSymbols.GenerateRandomSymbols(9); user.PasswordSalt = Convert.ToBase64String(salt); user.PasswordHash = SecurityHelper.HashPassword(password, salt); _context.Users.Update(user); await _emailService.SendPasswordRecoveryMail(email, password); await _context.SaveChangesAsync(); }