コード例 #1
0
        public async Task <IActionResult> Recovery(EmailRequest Email)
        {
            if (string.IsNullOrEmpty(Email.Email))
            {
                return(BadRequest(new { Data = "El usuario ingresado no existe", Success = false }));
            }

            var user = _dataContext.Users.First(x => x.UserName == Email.Email);

            if (!user.State)
            {
                return(BadRequest(new { Data = "Usuario inactivo", Success = false }));
            }

            var           longitud   = 7;
            string        caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            StringBuilder res        = new StringBuilder();
            Random        rnd        = new Random();

            while (0 < longitud--)
            {
                res.Append(caracteres[rnd.Next(caracteres.Length)]);
            }
            var response = await _userHelper.ChangePasswordRecovery(user, res.ToString());

            if (!response.Succeeded)
            {
                return(BadRequest(new { Data = "Hubo un error al guardar la contraseña", Success = false }));
            }

            try
            {
                await _dataContext.SaveChangesAsync();
            }catch (Exception ex)
            {
                return(BadRequest(new { Data = ex.ToString(), Success = false }));
            }

            SmtpClient client = new SmtpClient("smtp.gmail.com");

            client.UseDefaultCredentials = false;
            client.Credentials           = new NetworkCredential("*****@*****.**", "ctkubmxctgykbgge");
            client.EnableSsl             = true;
            MailMessage mailMessage = new MailMessage();

            mailMessage.From = new MailAddress("*****@*****.**");
            mailMessage.To.Add(Email.Email);
            mailMessage.Body    = res.ToString();
            mailMessage.Subject = "Codigo para recuperación de contraseña";
            client.Send(mailMessage);

            return(Ok(new { Data = "Codigo enviado correctamente", Success = true }));
        }