public ActionResult Register([Bind(Exclude = "IsEmailVerified,ActivationCode")] User registatingUser) { bool status = false; string message = ""; if (ModelState.IsValid) { //register the user if the name or the email doesn't exist if (!registatingUser.IsEmailOrNameExist(ref message)) { #region Generate Activation Code registatingUser.ActivationCode = Guid.NewGuid(); #endregion #region Password Hashing registatingUser.Password = Crypto.GetHash(registatingUser.Password); registatingUser.ConfirmPassword = registatingUser.Password; #endregion registatingUser.IsEmailVerified = false; #region Save to the Database using (RunetSoftDbEntities dbContext = new RunetSoftDbEntities()) { try { dbContext.tblUsers.Add(registatingUser); dbContext.SaveChanges(); } catch (DbEntityValidationException ex) { status = true; ViewBag.Message = ex.Message; return(View(registatingUser)); } //send email to the user MailAgent.SendVerificationLinkEmail(registatingUser.Email, registatingUser.ActivationCode.ToString()); message = "Регистрация прошла успешно. Для дальнейшего использования аккаунта необходимо воспользоваться ссылкой, " + " отправленной на ваш e-mail:" + registatingUser.Email; status = true; } #endregion } } else { message = "Неверный запрос"; } ViewBag.Message = message; ViewBag.Status = status; return(View(registatingUser)); }
public ActionResult RestorePassword(string email) { //if the user is already authenticated - redirect to the home page if (Request.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } string message = null; bool status = false; //checking if the user has specified the email if (!string.IsNullOrEmpty(email)) { using (RunetSoftDbEntities dataContext = new RunetSoftDbEntities()) { var user = dataContext.tblUsers.Where(usr => usr.Email == email).FirstOrDefault(); //checking if the user exists in the database if (user != null) { //sending the link to user's email which will redirect to the OnPasswordRestoring view //and the user could change the pass there. If the Guid mathes of course user.ActivationCode = Guid.NewGuid(); dataContext.Configuration.ValidateOnSaveEnabled = false; dataContext.SaveChanges(); MailAgent.SendVerificationLinkEmail(user.Email, user.ActivationCode.ToString(), true); message = "На вашу почту отправлена ссылка для восстановления пароля."; status = true; } else { message = "Пользователь с таким почтовым адресом не зарегистрирован."; } } } else { message = "Укажите почтовый адрес, для восстановления пароля."; } ViewBag.Status = status; ViewBag.Message = message; return(View()); }