public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { try { 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")); } var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); var callbackUrl = Url.Action("ResetPassword", "Account", new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme); TemplateCreateOptions templateCreateOptions = new TemplateCreateOptions() { TemplateModel = new ForgotPasswordEmailModel() { FirstName = user.FirstName, LastName = user.LastName, CallBackURL = callbackUrl }, TemplateName = ForgotPasswordEmailModel.ForgotPasswordEmailName }; string[] recipents = { user.Email }; string body = TemplateHandler.Create(templateCreateOptions); _mailClient.SendMail("Forgot Password", recipents, body, null); return(RedirectToAction("ForgotPasswordConfirmation", "Account")); } catch (Exception Ex) { throw Ex; } // 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)); }
public async Task <ActionResult> ConfirmEmailSendLink(ForgotPasswordViewModel model) { if (ModelState.IsValid) { try { var user = await UserManager.FindByNameAsync(model.Email); //if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) if (user == null) { // Don't reveal that the user does not exist return(View("ConfirmEmailSendLinkConfirmation")); } var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); TemplateCreateOptions templateCreateOptions = new TemplateCreateOptions() { TemplateModel = new ConfirmEmailModel() { FirstName = user.FirstName, LastName = user.LastName, CallBackURL = callbackUrl }, TemplateName = ConfirmEmailModel.ConfirmEmailName }; string[] recipents = { user.Email }; string body = TemplateHandler.Create(templateCreateOptions); _mailClient.SendMail("Email Confirmation", recipents, body, null); return(RedirectToAction("ConfirmEmailSendLinkConfirmation", "Account")); } catch (Exception Ex) { throw Ex; } } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> RegisterUser(RegisterViewModel model) { try { if (ModelState.IsValid) { var user = new ApplicationUser { FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber }; var roleStore = new RoleStore <IdentityRole>(ApplicationDbContext.Create()); var roleManager = new RoleManager <IdentityRole>(roleStore); if (!await roleManager.RoleExistsAsync(model.RoleName)) { await roleManager.CreateAsync(new IdentityRole() { Name = model.RoleName }); } var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { if (!await UserManager.IsInRoleAsync(user.Id, model.RoleName)) { await UserManager.AddToRoleAsync(user.Id, model.RoleName); } //Send email to confirm email after signed up. var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); TemplateCreateOptions templateCreateOptions = new TemplateCreateOptions() { TemplateModel = new ConfirmEmailModel() { FirstName = user.FirstName, LastName = user.LastName, CallBackURL = callbackUrl }, TemplateName = ConfirmEmailModel.ConfirmEmailName }; string[] recipents = { user.Email }; string body = TemplateHandler.Create(templateCreateOptions); _mailClient.SendMail("Email Confirmation", recipents, body, null); //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); return(RedirectToAction("UserIndex", "User")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); } catch (Exception ex) { return(null); } }