/// <summary> /// Obtiene los datos del usuario creado y manda un email /// </summary> /// <param name="user"></param> /// <param name="emailBody"></param> /// <param name="path"></param> private void SendSecuriyEmail(Users user, EmailBodyEnum emailBody, ActionProcessUserEnum actionProcess) { string linkConfirmation = $"{configuration["Addresses:Domain"]}api/User/account/" + $"confirmemail/{user.UserName}/{WebUtility.UrlEncode(user.TokenConfirmation)}/{(int)actionProcess}"; SendEMailDto mailDto = new SendEMailDto() { Body = linkConfirmation, EmailFrom = configuration["MailCredentials:Email"], PasswordFrom = configuration["MailCredentials:Password"], EmailTo = user.Email, Subject = "Confirm email to login" }; MessageSender.SendEmail(mailDto, emailBody); }
/// <summary> /// Confirma el email por la url de seguridad /// </summary> /// <param name="username"></param> /// <param name="token"></param> /// <param name="actionEnum"></param> public void ConfirmEmail(string username, string token, ActionProcessUserEnum actionEnum) { try { if (username is null || token is null) { throw new BussinessException(HttpStatusCode.BadRequest, "Problemas al leer el token"); } bool userExist = this.userRepository.UserExist(username); if (!userExist) { throw new BussinessException(HttpStatusCode.Forbidden, "Credenciales incorrectas"); } string[] apiAuth = new string[] { configuration["ApiAuth:Issuer"], configuration["ApiAuth:Audience"], configuration["ApiAuth:SecretKey"] }; if (!Security.ValidateCurrentToken(token, apiAuth)) { throw new BussinessException(HttpStatusCode.Forbidden, "Este token ya no expiró"); } UserTokenDto confirmDto = new UserTokenDto() { Token = token, Username = username }; Users user = this.userRepository.FindUserByUserNameTokenConfirm(confirmDto); if (user is null) { throw new BussinessException(HttpStatusCode.Forbidden, "Credenciales incorrectas"); } switch (actionEnum) { case ActionProcessUserEnum.login: if (user.IsConfirmed) { throw new BussinessException("El email ya fue confirmado anteriormente"); } user.IsConfirmed = true; user.State = true; user.TokenConfirmation = null; break; case ActionProcessUserEnum.recoverPass: user.IsConfirmedChange = true; break; default: break; } user.UpdateDate = DateTime.Now; int userUpdated = this.userRepository.UpdateUser(user); if (userUpdated == 0) { throw new BussinessException($"Error al confirmar el email"); } SendEMailDto mailDto = new SendEMailDto() { Body = "Successful process", EmailFrom = configuration["MailCredentials:Email"], PasswordFrom = configuration["MailCredentials:Password"], EmailTo = user.Email, Subject = "Information process" }; MessageSender.SendEmail(mailDto, EmailBodyEnum.successfulProcess); } catch (BussinessException) { throw; } catch (Exception) { throw; } }