public async Task <IActionResult> ConfirmEmail(string userId, string code) { if (userId == null || code == null) { return(RedirectToAction(nameof(HomeController.Index), "Home")); } var user = await _userManager.FindByIdAsync(userId); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{userId}'."); } // var id = Int32.Parse(userId); var result = await _userManager.ConfirmEmailAsync(user, code); if (result.Succeeded) { var url = Request.Host.Value; var link = Url.Action( "Login", "Account", values: new { email = user.Email }, protocol: Request.Scheme ); //var link = $"https://{url}/Account/ConfirmEmail?userId={user.Id}&code={code}"; var htmlString = await razorViewToStringRenderer.RenderViewToStringAsync("EmailTemplate", new EmailTemplate { Title = "Congratulation", Body = $"<a target=\"_blank\" href=\"{HtmlEncoder.Default.Encode(link)}\">ScoreExec</a>", Message = " <p>Assalam-o-Alaikum</p> <p>Dear User</p> <p>You are approved by the admin</p> <p>User Name: </P>" + user.UserName + "<p>Email: </p>" + user.Email }); string subject = "Email Confirmation"; await _signInManager.SignInAsync(user, isPersistent : false); await EmailExtensions.Execute(user.Email, user.UserName, htmlString, subject); } return(RedirectToAction("ApprovedUser", "Account")); }
public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model) { if (ModelState.IsValid) { var user = await _userManager.FindByEmailAsync(model.Email); if (user == null || !(await _userManager.IsEmailConfirmedAsync(user))) { // Don't reveal that the user does not exist or is not confirmed return(RedirectToAction(nameof(ForgotPasswordConfirmation))); } // For more information on how to enable account confirmation and password reset please // visit https://go.microsoft.com/fwlink/?LinkID=532713 var code = await _userManager.GeneratePasswordResetTokenAsync(user); var url = Request.Host.Value; string adminEmail = model.Email; string subject = "Reset Password"; var link = $"https://{url}/Account/ResetPassword?userId={user.Id}&code={code}"; var htmlString = await razorViewToStringRenderer.RenderViewToStringAsync("EmailTemplate", new EmailTemplate { Title = "Reset Password", Body = $"<a target=\"_blank\" href=\"{link}\">Click here</a>", Message = "<p>Asslam-o-Alaikum</p> <p>Dear User</p> <p>Click the below link to change or reset your passowrd</p>" }); await EmailExtensions.Execute(adminEmail, null, htmlString, subject); //var callbackUrl = Url.ResetPasswordCallbackLink(user.Id, code, Request.Scheme); //await _emailSender.SendEmailAsync(model.Email, "Reset Password", //$"Please reset your password by clicking here: <a href='{callbackUrl}'>link</a>"); return(RedirectToAction(nameof(PendingResertPasswordRequest))); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var url = Request.Host.Value; var link = Url.Action( "ConfirmEmail", "Account", values: new { userId = user.Id, code = code }, protocol: Request.Scheme ); //var link = $"https://{url}/Account/ConfirmEmail?userId={user.Id}&code={code}"; var htmlString = await razorViewToStringRenderer.RenderViewToStringAsync("EmailTemplate", new EmailTemplate { Title = "Email Confirmation", Body = $"<a target=\"_blank\" href=\"{HtmlEncoder.Default.Encode(link)}\">Yes it belongs to me</a>", Message = " <p>Assalam-o-Alaikum</p> <p>Hello Admin</p> <p>Please confirm, if this user " + model.UserName + " with the email " + model.Email + " belongs to you</P>" }); string adminEmail = "*****@*****.**"; string subject = "Email Confirmation"; //if (model.RoleName == "Club User") //{ // adminEmail = _context.ClubAdmins // .AsNoTracking() // .Where(i => i.TeamId == model.TeamId) // .Select(i => i.User.Email) // .Single(); //} //else //{ // adminEmail = "*****@*****.**"; //} await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); // await _userManager.AddToRoleAsync(user, model.RoleName); await EmailExtensions.Execute(adminEmail, model.UserName, htmlString, subject); return(RedirectToAction("PendingRequest", "Account")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }