public IActionResult ForgotPassword([FromBody] JObject json) { try { JObject jObject = JsonConvert.DeserializeObject <JObject>(DBHelper.ParseString(json)); if (jObject != null) { string emailId = DBHelper.ParseString(jObject["emailId"]); string IsAdmin = DBHelper.ParseString(jObject["IsAdmin"]); if (!string.IsNullOrWhiteSpace(emailId)) { if (iforgot.IsEmailExist(emailId, IsAdmin)) { string resetCode = RandomHelpers.RandomString(); long isUpdated = iforgot.UpdateForgotPasswordString(resetCode, emailId); if (isUpdated > 0) { var HostingPath = env.ContentRootPath; string clientEmail = config.GetValue <string>("app_settings:ClientEmail"); string clientEmailPassword = config.GetValue <string>("app_settings:ClientEmailPassword"); string port = config.GetValue <string>("app_settings:Port"); string mailHost = config.GetValue <string>("app_settings:SMTPURL"); string host = this.Request.Host.Value; string scheme = this.Request.Scheme; ForgotPasswordHelper.SendResetPasswordMail(emailId, resetCode, HostingPath, clientEmail, clientEmailPassword, port, mailHost, host, scheme); return(Ok(ResponseHelper.Success(MessageConstants.ResetPassordLink))); } else { return(Ok(ResponseHelper.Error(MessageConstants.DataNotFound))); } } else { return(Ok(ResponseHelper.Error(MessageConstants.CheckEmailId))); } } else { return(Ok(ResponseHelper.Error(MessageConstants.CompulsoryData))); } } else { return(Ok(ResponseHelper.Error(MessageConstants.CompulsoryData))); } } catch (Exception ex) { LogHelper.ExceptionLog(ex.Message + " ::::: " + ex.StackTrace); return(Ok(ResponseHelper.Error(ex.Message))); } }
public void When_forgot_password_with_incorrect_email() { var mockEmailManager = new Mock<EmailHelper>(); mockEmailManager.Setup(x => x.SendForgotPasswordEmail(It.IsAny<UserInfo>(), It.IsAny<string>())) .Returns(true); var mockDatabaseAccess = new Mock<IUserRepository>(); mockDatabaseAccess.Setup(x => x.GetUserByEmailAsync(It.IsAny<string>())).ThrowsAsync(new Exception()); var forgotPasswordHelper = new ForgotPasswordHelper(mockDatabaseAccess.Object, mockEmailManager.Object); Assert.ThrowsAsync<Exception>(async () => await forgotPasswordHelper.SendEmail("*****@*****.**")); }
public async Task <IActionResult> Post(string email) { try { var forgotPasswordHelper = new ForgotPasswordHelper(); var result = await forgotPasswordHelper.SendEmail(email); if (result == false) { return(StatusCode(500, "An error occurred while trying to send the password reset email. Please try again later.")); } return(Ok()); } catch (Exception ex) { return(BadRequest("User with that email does not exist.")); } }
public async Task When_forgot_password() { var mockDatabaseAccess = new Mock<IUserRepository>(); mockDatabaseAccess.Setup(x => x.GetUserByEmailAsync(It.IsAny<string>())).ReturnsAsync(new UserInfo { Email = "*****@*****.**", Password = "******" }); var mockEmailHelper = new Mock<EmailHelper>(); mockEmailHelper.Setup(x => x.SendForgotPasswordEmail(It.IsAny<UserInfo>(), It.IsAny<string>())) .Returns(true); var forgotPasswordHelper = new ForgotPasswordHelper(mockDatabaseAccess.Object, mockEmailHelper.Object); var result = await forgotPasswordHelper.SendEmail("*****@*****.**"); Assert.That(result, Is.True); }
static void Main(string[] args) { while (true) { Console.WriteLine("Welcome to the app"); if (loggedInUser.Equals("")) { Console.WriteLine("1. Login"); } else { Console.WriteLine("1. Continue as " + loggedInUser); } Console.WriteLine("2. Signup"); Console.WriteLine("3. Forgot Password"); int userInput = Convert.ToInt32(Console.ReadLine()); switch (userInput) { case 1: string userName; string password; if (sessionUser == null) { Console.WriteLine("Enter your username"); userName = Console.ReadLine(); Console.WriteLine("Enter your password"); password = Console.ReadLine(); LoginHelper authenticator = new LoginHelper(); try { if (authenticator.LoginValidator(userName, password) == null) { Console.WriteLine("Invalid Login or the user does not exist"); } else { Console.WriteLine("Succesful Login"); loggedInUser = userName; sessionUser = authenticator.LoginValidator(userName, password); Menu(); } } catch (Exception e) { Console.WriteLine(e.Message); } } else { Menu(); } break; case 2: Console.WriteLine("Enter a username"); userName = Console.ReadLine(); Console.WriteLine("Enter a password"); password = Console.ReadLine(); Console.WriteLine("What was the name of your first school?"); string Answer = Console.ReadLine(); SignupHelper signupService = new SignupHelper(userName); try { sessionUser = signupService.SignupService(userName, password, Answer); loggedInUser = userName; Menu(); } catch (Exception e) { Console.WriteLine(e.Message); } break; case 3: ForgotPasswordHelper resetPasswordService = new ForgotPasswordHelper(); Console.WriteLine("Enter the user name of the account"); userName = Console.ReadLine(); try { Console.WriteLine("What is the name of your first school?"); string answer = Console.ReadLine(); resetPasswordService.ForgotPasswordService(userName, answer); } catch (Exception e) { Console.WriteLine(e.Message); } break; } } }