public async Task <ActionResult> RegisterUser(RegisterViewModel model, Webshop.BL.EmailService service) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, PhoneNumber = model.PhoneNumber, Firstname = model.Firstname, Surname = model.Surname, Address = model.Address, ZIPCode = model.ZIPCode, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { // Comment the following line to prevent log in until the user is confirmed. //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); //Assign Role to user Here await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); service.SendMail(user.Email, user.Id, "Confirm your account", "Please confirm your account by clicking " + callbackUrl); //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); ViewBag.Message = "Check your email and confirm your account, you must be confirmed " + "before you can log in."; return(View("Info")); //return RedirectToAction("Index", "Home"); } ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Customer")) .ToList(), "Name", "Name"); AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordViewModel model, Webshop.BL.EmailService service) { 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 https://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); service.SendMail(user.Email, user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>"); //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)); }