public ActionResult ResetPasswordRequest(IdentityViewModel viewModel) { if (string.IsNullOrWhiteSpace(viewModel.PracticeIdentifier)) return this.View(viewModel); if (string.IsNullOrWhiteSpace(viewModel.UserNameOrEmail)) return this.View(viewModel); // Can only reset password if practice has already been verified. var practice = this.db.Practices.SingleOrDefault(p => p.UrlIdentifier == viewModel.PracticeIdentifier); var user = SecurityManager.GetUser(this.db.Users, viewModel.PracticeIdentifier, viewModel.UserNameOrEmail); if (practice == null || user == null) { this.ModelState.ClearPropertyErrors(() => viewModel.PracticeIdentifier); this.ModelState.ClearPropertyErrors(() => viewModel.UserNameOrEmail); this.ModelState.AddModelError( () => viewModel.PracticeIdentifier, "O consultório ou usuário não existem. Por favor verifique se não cometeu nenhum erro de digitação."); } if (practice != null && practice.VerificationDate == null && user != null) { this.ModelState.AddModelError( () => viewModel.PracticeIdentifier, "Não é possível resetar a senha pois o consultório ainda não foi verificado. " + "Confirme o seu e-mail antes de tentar mudar a senha."); } if (this.ModelState.IsValid) { var utcNow = this.GetUtcNow(); // Creating confirmation email, with the token. MailMessage message; if (user.Person.Email != null) { #region Creating token and e-mail message // Setting verification token. // Note: tokens are safe to save even if validation fails. TokenId tokenId; using (var db2 = this.CreateNewCerebelloEntities()) { var token = db2.GLB_Token.CreateObject(); token.Value = Guid.NewGuid().ToString("N"); token.Type = "ResetPassword"; token.Name = string.Format( "Practice={0}&UserName={1}", user.Practice.UrlIdentifier, user.UserName); token.ExpirationDate = utcNow.AddDays(Constants.MAX_DAYS_TO_RESET_PASSWORD); db2.GLB_Token.AddObject(token); db2.SaveChanges(); tokenId = new TokenId(token.Id, token.Value); } // Rendering message bodies from partial view. var emailViewModel = new UserEmailViewModel(user) { Token = tokenId.ToString(), }; var toAddress = new MailAddress(user.Person.Email, user.Person.FullName); message = this.CreateEmailMessagePartial("ResetPasswordEmail", toAddress, emailViewModel); #endregion } else { return this.RedirectToAction("ResetPasswordManually"); } // If the ModelState is still valid, then save objects to the database, // and send confirmation email message to the user. using (message) { if (this.ModelState.IsValid) { try { // Sending the password reset e-mail to the user. this.TrySendEmail(message); } catch (SmtpException) { // if e-mail was not sent, try to send it again, after 10 seconds Thread.Sleep(10000); this.TrySendEmail(message); } return this.RedirectToAction("ResetPasswordEmailSent"); } } } return this.View(viewModel); }
private MailMessage EmailMessageToUser(User user, DateTime utcNow, bool isTrial) { TokenId tokenId; // Setting verification token. using (var db2 = this.CreateNewCerebelloEntities()) { var token = new GLB_Token(); token.Value = Guid.NewGuid().ToString("N"); token.Type = "VerifyPracticeAndEmail"; token.Name = string.Format("Practice={0}&UserName={1}", user.Practice.UrlIdentifier, user.UserName); token.ExpirationDate = utcNow.AddHours(Constants.MAX_HOURS_TO_VERIFY_TRIAL_ACCOUNT); db2.GLB_Token.AddObject(token); db2.SaveChanges(); tokenId = new TokenId(token.Id, token.Value); } // Rendering message bodies from partial view. var emailViewModel = new UserEmailViewModel(user) { Token = tokenId.ToString(), IsTrial = isTrial }; var toAddress = new MailAddress(user.Person.Email, user.Person.FullName); var emailMessageToUser = this.CreateEmailMessage("ConfirmationEmail", toAddress, emailViewModel); return emailMessageToUser; }