// POST: /Account/ForgotPassword [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await UserManager.FindByNameAsync(model.Email); MembershipUser userEmail; using (var context = new LolaBikeContext()) { var foundUserName = (from u in context.userProfiles where u.Email == model.Email select u.UserName).FirstOrDefault(); if (foundUserName != null) { userEmail = Membership.GetUser(foundUserName.ToString()); } else { user = null; } } if (user != null) { // Generae password token that will be used in the email link to authenticate user var token = WebSecurity.GeneratePasswordResetToken(user.UserName); // Generate the html link sent via email string resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { rt = token }, "http") + "'>Reset Password Link</a>"; // Email stuff string subject = "Reset your password for asdf.com"; string body = "You link: " + resetLink; string from = "email"; MailMessage message = new MailMessage(from, model.Email); message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient { Host = "smtp.sendgrid.net", Port = portnumber, EnableSsl = false, UseDefaultCredentials = false, Credentials = new System.Net.NetworkCredential("username", "password"), DeliveryMethod = SmtpDeliveryMethod.Network }; // Attempt to send the email try { client.Send(message); } catch (Exception e) { ModelState.AddModelError("", "Issue sending email: " + e.Message); } } return View("ForgotPasswordConfirmation"); //TempData["Message"] = "User Not exist."; } // If we got this far, something failed, redisplay form return View(model); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)