public ActionResult ConfirmEmail(int?ConfirmationCode) { if (!LoggedIn()) { return(RedirectToAction("Login", "Auth")); } if (isAdmin()) { return(RedirectToAction("Index", "Admin")); } if (!isAdmin() && !isAuthor()) { return(RedirectToAction("Logout", "Auth")); } Author author = authorHelper.GetById(GetAuthorId()); if (author.isEmailConfirmed) { return(RedirectToAction("Index")); } ViewBag.Email = author.Email; if (ConfirmationCode == null) { ViewBag.Error = "Invalid confirmation code!"; return(View()); } if (ConfirmationCode.Value.ToString().Length != 6) { ViewBag.Error = "Invalid confirmation code!"; return(View()); } EmailCode emailCode = emailCodeHelper.GetAll().SingleOrDefault(e => e.AuthorId == author.Id && e.Email == author.Email && !e.isExpired); if (emailCode != null) { author.isEmailConfirmed = true; authorHelper.Update(author); emailCode.isExpired = true; emailCodeHelper.Update(emailCode); return(RedirectToAction("Index")); } else { ViewBag.Error = "Invalid confirmation code!"; } return(View()); }
public void SendCode(int AuthorId) { Author author = authorHelper.GetById(AuthorId); // at this point, author must have a new email address to be verified author.isEmailConfirmed = false; authorHelper.Update(author); // invalidate all other previous EmailCodes of same author var oldConfirmations = emailCodeHelper.GetAll().Where(ec => ec.AuthorId == author.Id && !ec.isExpired); foreach (EmailCode code in oldConfirmations) { code.isExpired = true; emailCodeHelper.Update(code); } // 6 digit random code int confirmation_number = new Random().Next(100000, 999999); // save code to db emailCodeHelper.Create(new EmailCode() { AuthorId = author.Id, Email = author.Email, ConfirmationNumber = confirmation_number, isExpired = false }); // send email to author string title = "Confirmation number"; string body = "Dear " + author.FullName + "\nYour confirmation number is: " + confirmation_number; SendMail(author.Email, title, body); }
public ActionResult VerifyAuthor(int id) { if (!LoggedIn()) { return(RedirectToAction("LoginAdmin", "Auth")); } if (!isAdmin()) { return(RedirectToAction("Index", "Author")); } try { Author author = authorHelper.GetById(id); author.isVerified = true; authorHelper.Update(author); } catch (Exception) { } return(RedirectToAction("Authors")); }