public IActionResult ForgotPassword(ForgotPassDto model) { var user = _accRepo.GetAccountByEmail(model.Email); //EmailConfig SmtpClient client = new SmtpClient("smtp.gmail.com", 587); client.EnableSsl = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("*****@*****.**", "abcdEcosystem123"); MailMessage msg = new MailMessage(); //Token config var Token = Guid.NewGuid().ToString(); var encodedToken = _authRepo.TokenConfig(Token); if (user != null) { //Adding table data to db var modifiedData = _authRepo.AddToken(Token, user); if (user == null) { return(NotFound()); } _mapper.Map(modifiedData, user); _accRepo.UpdateAccount(user); _accRepo.SaveChanges(); //Sending Email with query parameters string url = $"{_configuration["ClientAppUrl"]}/resetpassword?email={model.Email}&token={encodedToken}"; msg.To.Add(model.Email); msg.From = new MailAddress("UST Eco-Tigers <*****@*****.**>"); msg.Subject = "Password Reset Url"; msg.Body = url; client.Send(msg); return(Ok()); } return(BadRequest()); }
public async Task <ActionResult> ForgotPassword(ForgotPassDto model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var intialInfo = await GetTenantDbInfo(model.HostName); if (intialInfo == null || string.IsNullOrEmpty(intialInfo.TenantDBServer)) { ModelState.AddModelError("", "Invalid Host Name."); return(BadRequest(ModelState)); } bool user = await _loginService.AuthenticateUserByEmail(intialInfo.GetConnectionString(), model.EmailAddress); if (!user) { return(StatusCode(StatusCodes.Status404NotFound, "Email address in not valid.")); } var token = await _loginService.GeneratePasswordResetToken(intialInfo.GetConnectionString(), model.EmailAddress); _loginService.SendForgotPasswordEmail(token, model.EmailAddress); //_emailNotificationService.SendAsyncEmail(model.EmailAddress, "Password reset token", token, true); return(Ok()); } catch (Exception ex) { _logger.LogError(ex.StackTrace); return(StatusCode(StatusCodes.Status500InternalServerError, "Something went wrong!")); } }