public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { string username; try { username = (from c in appcontext.Users where c.Email.Equals(model.Email) select c.UserName).Single().ToString(); } catch { return View("ForgotPasswordConfirmation"); } var user = await UserManager.FindByNameAsync(username); if (user == null) { // Don't reveal that the user does not exist or is not confirmed return View("ForgotPasswordConfirmation"); } if (user.EmailConfirmed == false) { string emailcode = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var emailcallbackurl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = emailcode }, protocol: Request.Url.Scheme); var response = SendEmailConfirmation(user.Email, emailcallbackurl, true); return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); //Assemble message first. var body = "<h2>Reset your password for Blue Ribbons Review!</h2>" + "<p><strong>Use the following link to reset your password:</strong></p>" + "<p><a href='" + callbackUrl + "'>Reset Password for Blue Ribbons Review!</p>"; //SendMessage RestClient client = new RestClient(); client.BaseUrl = new Uri("https://api.mailgun.net/v3"); client.Authenticator = new HttpBasicAuthenticator("api", WebConfigurationManager.AppSettings["mailSecretKey"]); RestRequest request = new RestRequest(); request.AddParameter("domain", "blueribbonsreview.com", ParameterType.UrlSegment); request.Resource = "{domain}/messages"; //Who will be displayed as sender, put info here! request.AddParameter("from", "Blue Ribbons Review <*****@*****.**>"); //Who will be receiving messages, put email here. request.AddParameter("to", model.Email); request.AddParameter("subject", "Blue Ribbons Review - Password Reset"); request.AddParameter("html", body); request.Method = Method.POST; var result = client.Execute(request); return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // If we got this far, something failed, redisplay form return View(model); }
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) { // Don't reveal that the user does not exist or is not confirmed return View("ForgotPasswordConfirmation"); } // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); // var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); // return RedirectToAction("ForgotPasswordConfirmation", "Account"); } // If we got this far, something failed, redisplay form return View(model); }