public async Task <VerifyEmailInfo> GetEmailVerificationInfo(Guid userId) { IUserContext userContext = null; string token = null; var user = await userManager.Fetch(userManager.Site.Id, userId); if (user != null) { token = await userManager.GenerateEmailConfirmationTokenAsync((SiteUser)user); userContext = new UserContext(user); } return(new VerifyEmailInfo(userContext, token)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new SiteUser { UserName = model.Email, Email = model.Email }; var result = await _siteUserManager.CreateAsync(user); // var result = await _siteUserManager.CreateAsync(user, model.Password); // var user = await _userManager.FindByEmailAsync(model.Email); if (result.Succeeded) { // Send an email with this link var code = await _siteUserManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action(nameof(EmailClient), "Email", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); //Email from Email Template string Message = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"; // string body; var webRoot = _env.WebRootPath; //get wwwroot Folder //Get TemplateFile located at wwwroot/Templates/EmailTemplate/Register_EmailTemplate.html var pathToFile = _env.WebRootPath + Path.DirectorySeparatorChar.ToString() + "Templates" + Path.DirectorySeparatorChar.ToString() + "EmailTemplate" + Path.DirectorySeparatorChar.ToString() + "Confirm_Account_Registration.html"; var subject = "Confirm Account Registration"; var builder = new BodyBuilder(); using (StreamReader SourceReader = System.IO.File.OpenText(pathToFile)) { builder.HtmlBody = SourceReader.ReadToEnd(); } //{0} : Subject //{1} : DateTime //{2} : Email //{3} : Username //{4} : Password //{5} : Message //{6} : callbackURL string messageBody = string.Format(builder.HtmlBody, subject, String.Format("{0:dddd, d MMMM yyyy}", DateTime.Now), model.Email, model.Email, model.Password, Message, callbackUrl ); await _emailSender.SendEmailAsync("*****@*****.**", model.Email, model.Email, subject, messageBody); ViewData["Message"] = $"Please confirm your account by clicking this link: <a href='{callbackUrl}' class='btn btn-primary'>Confirmation Link</a>"; ViewData["MessageValue"] = "1"; _logger.LogInformation(3, "User created a new account with password."); return(LocalRedirect(returnUrl)); } ViewData["Message"] = $"Error creating user. Please try again later"; ViewData["MessageValue"] = "0"; // AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel model) { ViewData["Title"] = sr["Register"]; if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0)) { model.RecaptchaSiteKey = Site.RecaptchaPublicKey; } model.UseEmailForLogin = Site.UseEmailForLogin; model.RegistrationPreamble = Site.RegistrationPreamble; model.RegistrationAgreement = Site.RegistrationAgreement; model.AgreementRequired = Site.RegistrationAgreement.Length > 0; model.ExternalAuthenticationList = signInManager.GetExternalAuthenticationSchemes(); bool isValid = ModelState.IsValid; if (isValid) { if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0)) { string recpatchaSecretKey = Site.RecaptchaPrivateKey; var captchaResponse = await this.ValidateRecaptcha(Request, recpatchaSecretKey); if (!captchaResponse.Success) { //if (captchaResponse.ErrorCodes.Count <= 0) //{ // return View(model); //} ////TODO: log these errors rather than show them in the ui //var error = captchaResponse.ErrorCodes[0].ToLower(); //switch (error) //{ // case ("missing-input-secret"): // ModelState.AddModelError("recaptchaerror", "The secret parameter is missing."); // break; // case ("invalid-input-secret"): // ModelState.AddModelError("recaptchaerror", "The secret parameter is invalid or malformed."); // break; // case ("missing-input-response"): // ModelState.AddModelError("recaptchaerror", "The response parameter is missing."); // break; // case ("invalid-input-response"): // ModelState.AddModelError("recaptchaerror", "The response parameter is invalid or malformed."); // break; // default: // ModelState.AddModelError("recaptchaerror", "Error occured. Please try again"); // break; //} ModelState.AddModelError("recaptchaerror", "reCAPTCHA Error occured. Please try again"); isValid = false; } } if (Site.RegistrationAgreement.Length > 0) { if (!model.AgreeToTerms) { ModelState.AddModelError("agreementerror", sr["You must agree to the terms"]); isValid = false; } } var userName = model.Username.Length > 0 ? model.Username : model.Email.Replace("@", string.Empty).Replace(".", string.Empty); var userNameAvailable = await userManager.LoginIsAvailable(Guid.Empty, userName); if (!userNameAvailable) { ModelState.AddModelError("usernameerror", sr["Username not accepted please try a different value"]); isValid = false; } if (!isValid) { return(View(model)); } var user = new SiteUser { UserName = userName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName, AccountApproved = Site.RequireApprovalBeforeLogin ? false : true }; if (model.DateOfBirth.HasValue) { user.DateOfBirth = model.DateOfBirth.Value; } var result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await ipAddressTracker.TackUserIpAddress(Site.Id, user.Id); if (Site.RequireConfirmedEmail) // require email confirmation { var code = await userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action(new UrlActionContext { Action = "ConfirmEmail", Controller = "Account", Values = new { userId = user.Id.ToString(), code = code }, Protocol = HttpContext.Request.Scheme }); emailSender.SendAccountConfirmationEmailAsync( Site, model.Email, sr["Confirm your account"], callbackUrl).Forget(); if (this.SessionIsAvailable()) { this.AlertSuccess(sr["Please check your email inbox, we just sent you a link that you need to click to confirm your account"], true); return(Redirect("/")); } else { return(RedirectToAction("EmailConfirmationRequired", new { userId = user.Id, didSend = true })); } } else { if (Site.RequireApprovalBeforeLogin) { emailSender.AccountPendingApprovalAdminNotification(Site, user).Forget(); return(RedirectToAction("PendingApproval", new { userId = user.Id, didSend = true })); } else { await signInManager.SignInAsync(user, isPersistent : false); return(this.RedirectToSiteRoot(Site)); } } } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public async Task <IActionResult> Register(RegisterViewModel model) { ViewData["Title"] = "Register"; if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0)) { model.RecaptchaSiteKey = Site.RecaptchaPublicKey; } model.RegistrationPreamble = Site.RegistrationPreamble; model.RegistrationAgreement = Site.RegistrationAgreement; bool isValid = ModelState.IsValid; if (isValid) { if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0)) { string recpatchaSecretKey = Site.RecaptchaPrivateKey; var captchaResponse = await this.ValidateRecaptcha(Request, recpatchaSecretKey); if (!captchaResponse.Success) { //if (captchaResponse.ErrorCodes.Count <= 0) //{ // return View(model); //} ////TODO: log these errors rather than show them in the ui //var error = captchaResponse.ErrorCodes[0].ToLower(); //switch (error) //{ // case ("missing-input-secret"): // ModelState.AddModelError("recaptchaerror", "The secret parameter is missing."); // break; // case ("invalid-input-secret"): // ModelState.AddModelError("recaptchaerror", "The secret parameter is invalid or malformed."); // break; // case ("missing-input-response"): // ModelState.AddModelError("recaptchaerror", "The response parameter is missing."); // break; // case ("invalid-input-response"): // ModelState.AddModelError("recaptchaerror", "The response parameter is invalid or malformed."); // break; // default: // ModelState.AddModelError("recaptchaerror", "Error occured. Please try again"); // break; //} ModelState.AddModelError("recaptchaerror", "reCAPTCHA Error occured. Please try again"); isValid = false; } } //if (Site.RegistrationAgreement.Length > 0) //{ // if (!model.AgreeToTerms) // { // ModelState.AddModelError("agreementerror", "You must agree to the terms"); // isValid = false; // } //} if (!isValid) { return(View(model)); } var user = new SiteUser { UserName = model.LoginName.Length > 0? model.LoginName : model.Email.Replace("@", string.Empty).Replace(".", string.Empty), Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, DisplayName = model.DisplayName }; if (model.DateOfBirth.HasValue) { user.DateOfBirth = model.DateOfBirth.Value; } var result = await userManager.CreateAsync(user, model.Password); if (result.Succeeded) { if (Site.UseSecureRegistration) // require email confirmation { var code = await userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); await emailSender.SendAccountConfirmationEmailAsync( Site, model.Email, "Confirm your account", callbackUrl); if (this.SessionIsAvailable()) { this.AlertSuccess("Please check your email inbox, we just sent you a link that you need to click to confirm your account", true); return(Redirect("/")); } else { return(RedirectToAction("EmailConfirmationRequired", new { userGuid = user.Id, didSend = true })); } } else { if (Site.RequireApprovalBeforeLogin) { //TODO: send notification to admins about request for approval } else { await signInManager.SignInAsync(user, isPersistent : false); return(Redirect("/")); } } } AddErrors(result); } //else //{ // this.AlertDanger("model was invalid", true); //} // If we got this far, something failed, redisplay form return(View(model)); }