protected void cmdForgotPassword_Click(object sender, EventArgs e) { ForgotPassword myFP = ForgotPassword.GetByEmail(txtInputEmail.Text); if (myFP != null) { if ((DateTime.Now - myFP.DateRequested).Hours > 0) { // This forgotpassword link is outdated and should be deleted. Reset to null so it's treated // as a new record. myFP.Delete(); myFP = null; } } if (myFP == null) { Player myPlayer = Player.GetAllPlayers().FirstOrDefault(x => x.Email == txtInputEmail.Text); if (myPlayer != null) { cmdForgotPassword.Text = "Please Wait..."; cmdForgotPassword.Enabled = false; myFP = new ForgotPassword(txtInputEmail.Text); //Generate a new password here string newPassword = makeRandomString(10); myPlayer.Password = newPassword; myPlayer.SaveToDB(); Email.SendMail("Five Oaths LRP", txtInputEmail.Text, "Your password has been reset", "We have reset your password to " + newPassword + " please login with this password"); Log.Write(myPlayer.PlayerID, "Reset password"); lblErrorMessage.Text = "An email has been sent to you with your new password"; lblErrorMessage.Visible = true; cmdForgotPassword.Visible = false; } else { lblErrorMessage.Text = "No account exists with that email address"; lblErrorMessage.Visible = true; } } else { lblErrorMessage.Text = "An existing forgot password link exists for this email. Please wait and try again"; lblErrorMessage.Visible = true; } }
public ActionResult ResetPassword(UserProfile lc) { try { ForgotPassword fg = new ForgotPassword(); fg.LoadByUserId(lc.User.Id); if (fg != null) { if (DateTime.Now > fg.ExpirationDate) { ViewBag.Message = "Reset Link has Expired"; fg.Delete(); return(View(lc)); } else { if (lc.User.Password == null) { ModelState.AddModelError(string.Empty, "Password is required"); } if (lc.User.Password == null) { ModelState.AddModelError(string.Empty, "Password is required"); } else if (lc.User.Password.Length < 6) { ModelState.AddModelError(string.Empty, "Password needs to be at least 6 characters"); } else if (lc.User.Password.Length > 16) { ModelState.AddModelError(string.Empty, "Password needs to be less than 16 characters"); } else if (lc.ConfirmPassword != lc.User.Password) { ModelState.AddModelError(string.Empty, "Passwords did not match"); } if (!ModelState.IsValid) { return(View(lc)); } else { if (lc.ConfirmPassword == lc.User.Password) { lc.User.ChangeForgottenPassword(lc.User.Password); //DELETE ON SUCCESSFUL USE fg.Delete(); return(RedirectToAction("ResetPasswordSuccess", "Login")); } else { ViewBag.Message = "Passwords need to match"; return(View(lc)); } } } } else { ViewBag.Message = "Reset Link Doesn't Exist"; return(View(lc)); } } catch { ViewBag.Message = "Error Occured While Attempted to Reset Password"; return(View(lc)); } }