/// <summary> /// Author: BOS Framework, Inc /// Description: Triggers when the Register button is clicked /// </summary> /// <param name="forgotPasswordObj"></param> /// <returns></returns> public async Task <ActionResult> ForgotPasswordAction(ForgotPassword forgotPasswordObj) { try { if (HttpContext != null && !HttpContext.Request.Cookies.ContainsKey(".AspNet.Consent")) { if (_bosAuthClient == null) { var response = await _multitenantService.GetGeneratedToken(); } ModelState.AddModelError("CustomError", "Before proceeding, please 'Accept' our Cookies' terms."); return(View("ForgotPassword")); } if (ModelState.IsValid) { string emailAddress = forgotPasswordObj.EmailAddress.Trim(); //Trimming the email input if (forgotPasswordObj != null) { if (_bosAuthClient == null) { var response = await _multitenantService.GetGeneratedToken(); return(RedirectToAction("ForgotPassword")); } var userResponse = await _bosAuthClient.GetUserByEmailAsync <BOSUser>(emailAddress); //Mkaing a call to the BOS API to validate the entered email address if (userResponse != null && userResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("SignOut", "Auth")); } if (userResponse != null && userResponse.Users != null && userResponse.Users.Count > 0) { var slugResponse = await _bosAuthClient.CreateSlugAsync(emailAddress); //On success, creating a slug object that will be used while resetting the password if (slugResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("SignOut", "Auth")); } if (slugResponse != null && slugResponse.IsSuccessStatusCode) { var slug = slugResponse.Slug; //Creating the email object to send the email Models.BOSModels.Email emailObj = new Models.BOSModels.Email { Deleted = false, From = new From { Email = "*****@*****.**", Name = "StarterCode Team", }, To = new List <To> { new To { Email = emailAddress, Name = "" } } }; var templateResponse = await _bosEmailClient.GetTemplateAsync <Template>(); if (templateResponse != null && templateResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("SignOut", "Auth")); } if (templateResponse != null && templateResponse.IsSuccessStatusCode) { emailObj.TemplateId = templateResponse.Templates.Where(i => i.Name == "ForgotPassword").Select(i => i.Id).ToList()[0]; } else { ModelState.AddModelError("CustomError", "Sorry! We could not send you an email. Please try again later"); return(View("Index")); } var spResponse = await _bosEmailClient.GetServiceProviderAsync <ServiceProvider>(true); if (spResponse != null && spResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("SignOut", "Auth")); } if (spResponse != null && spResponse.IsSuccessStatusCode) { emailObj.ServiceProviderId = spResponse.ServiceProvider[0].Id; } else { ModelState.AddModelError("CustomError", "Sorry! We could not send you an email. Please try again later"); return(View("Index")); } string hostUrl = _contextAccessor.HttpContext.Request.Host.ToString(); string baseUrl = string.Format("{0}://{1}", hostUrl.Contains("localhost") ? "http" : "https", hostUrl); string logoUrl = baseUrl + "/images/logo.png"; string appName = _configuration["ApplicationName"]; var appConfigSession = _contextAccessor.HttpContext.Session.GetString("ApplicationConfig"); if (appConfigSession != null) { var appconfig = JsonConvert.DeserializeObject <WhiteLabel>(appConfigSession); if (appconfig != null) { baseUrl = appconfig.URL; logoUrl = appconfig.Logo; appName = appconfig.Name; } } var userDetails = userResponse.Users.FirstOrDefault(); emailObj.Substitutions = new List <Substitution>(); emailObj.Substitutions.Add(new Substitution { Key = "companyUrl", Value = baseUrl }); emailObj.Substitutions.Add(new Substitution { Key = "companyLogo", Value = logoUrl }); emailObj.Substitutions.Add(new Substitution { Key = "usersName", Value = userDetails != null ? userDetails.Username.Split("@")[0] : "" }); emailObj.Substitutions.Add(new Substitution { Key = "applicationName", Value = appName }); emailObj.Substitutions.Add(new Substitution { Key = "resetUrl", Value = baseUrl + "/Password/Reset?slug=" + slug.Value + "&set=false" }); emailObj.Substitutions.Add(new Substitution { Key = "thanksCredits", Value = "Team StarterCode" }); var emailResponse = await _bosEmailClient.SendEmailAsync <IEmail>(emailObj); if (emailResponse != null && emailResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized) { return(RedirectToAction("SignOut", "Auth")); } if (!emailResponse.IsSuccessStatusCode) { ModelState.AddModelError("CustomError", emailResponse.BOSErrors[0].Message); return(View("Index")); } } } } else { } } //Even if the email adrress entered is not a valid one, we show the same sucess message. This is a form of securing the user's information ViewBag.Message = "Check your inbox for an email with a link to reset your password."; return(View("Index")); } catch (Exception ex) { Logger.LogException("Auth", "ForgotPasswordAction", ex); dynamic model = new ExpandoObject(); model.Message = ex.Message; model.StackTrace = ex.StackTrace; return(View("ErrorPage", model)); } }