public async Task <OTPResponse> SendOTP(SendOTPRequest oData) { OTPResponse sResponse = new OTPResponse(); try { var users = await _userData.FindByUsernameAsync(oData.userName); if (users.Count > 0) { var email = users[0].email; var mobile = users[0].contactNo; var userFullName = users[0].name; var otp = GenerateOTPNumber(); if (email == "") { sResponse.status = "false"; sResponse.message = "Email does not exist for this user"; } else { var otpSend = _userData.SendOTP(oData.userName, otp); if (otpSend.msgRespone == true) { var sent = _mailService.ForgotPasswordOTPMail(otp, userFullName, email); if (sent == true) { sResponse.status = "true"; sResponse.message = otpSend.msg; } else { sResponse.status = "false"; sResponse.message = "Mail sent Failed"; } } else { sResponse.status = "false"; sResponse.message = otpSend.msg; } } } else { sResponse.status = "false"; sResponse.message = "Username does not exist"; } return(sResponse); } catch (Exception e) { sResponse.status = "false"; sResponse.message = e.Message; } return(sResponse); }
public async Task <IActionResult> SendOTP(SendOTPRequest oData) { _logger.LogInformation($"Invoking endpoint: {this.HttpContext.Request.GetDisplayUrl()}"); var otp = await _usersService.SendOTP(oData); _logger.LogInformation($"Send otp for forgot passwords {otp}"); return(Ok(new OTPResponse { status = otp.status, message = otp.message, })); }
public async Task <OTPResponse> SendOTP(SendOTPRequest oData) { OTPResponse sResponse = new OTPResponse(); try { var users = await _usersData.FindByUsernameAsync(oData.userName); if (users.Count > 0) { var email = users[0].email; var mobile = users[0].mobileNo; var userFullName = users[0].name; var otp = GenerateOTPNumber(); var host = _config.GetSection("SMTPDetails").GetSection("host").Value; var port = _config.GetSection("SMTPDetails").GetSection("port").Value; var uName = _config.GetSection("SMTPDetails").GetSection("username").Value; string pwd = _config.GetSection("SMTPDetails").GetSection("pwd").Value; string from = _config.GetSection("SMTPDetails").GetSection("from").Value; string cc = _config.GetSection("ForgotPasswordSMTP").GetSection("recipients").Value; string recipients = email + cc; string subject = _config.GetSection("ForgotPasswordSMTP").GetSection("subject").Value; string body = _config.GetSection("ForgotPasswordSMTP").GetSection("message").Value + otp.ToString(); string mailTemplateSubject = _config.GetSection("ForgotPasswordMailTemplate").GetSection("Subject").Value; string mailTemplateBody = _config.GetSection("ForgotPasswordMailTemplate").GetSection("Body").Value; string mailBody = ""; //var forgetPasswordModel = JsonConvert.DeserializeObject<ForgetPassword>(mailTemplate); if (email == "") { sResponse.status = "false"; sResponse.message = "Email does not exist for this user"; } else { mailBody = ""; mailBody = mailBody + mailTemplateBody.Replace("#OTP", otp).Replace("#RecipientName", userFullName); var mailMessage = new MailMessage(from, recipients, mailTemplateSubject, mailBody); mailMessage.IsBodyHtml = true; var otpSend = _usersData.SendOTP(oData.userName, otp); if (otpSend.msgRespone == true) { var client = new SmtpClient(host, int.Parse(port)) { Credentials = new NetworkCredential(uName, pwd), EnableSsl = true }; client.Send(mailMessage); //client.Send(from, recipients, mailTemplateSubject, HttpUtility.HtmlDecode(mailBody)); sResponse.status = "true"; sResponse.message = otpSend.msg; } else { sResponse.status = "false"; sResponse.message = otpSend.msg; } } } else { sResponse.status = "false"; sResponse.message = "Username does not exist"; } return(sResponse); } catch (Exception e) { sResponse.status = "false"; sResponse.message = e.Message; } return(sResponse); }