コード例 #1
0
        public ActionResult ForgotUserName([Bind(Prefix = "forgotUserNameViewModel")] ForgotUserNameViewModel model)
        {
            try
            {
                Dictionary <string, string> dynamicText = new Dictionary <string, string>();
                // Attempt to get the member based on the given email address
                var member = Services.MemberService.GetByEmail(model.Email);

                // If a member with that email exists
                if (member != null)
                {
                    // Build a dictionary for all teh dynamic text in the email template
                    dynamicText = new Dictionary <string, string>
                    {
                        { "<%FirstName%>", member.GetValue("firstName").ToString() },
                        { "<%UserName%>", member.Username }
                    };
                }
                else
                {
                    // try get user from API
                    var hriUser = MakeInternalApiCallJson("GetRegisteredUserByEmail", new Dictionary <string, string> {
                        { "email", model.Email }
                    });
                    if (hriUser != null)
                    {
                        dynamicText = new Dictionary <string, string>
                        {
                            { "<%FirstName%>", hriUser["FirstName"].ToString() },
                            { "<%UserName%>", hriUser["UserName"].ToString() }
                        };
                    }
                }

                if (dynamicText.Any())
                {
                    // Get the Umbraco root node to access dynamic information (phone numbers, emails, ect)
                    IPublishedContent root = Umbraco.TypedContentAtRoot().First();

                    // add phone number to dynamic text
                    dynamicText.Add("<%PhoneNumber%>", root.GetProperty("phoneNumber").Value.ToString());

                    //Get the Verification Email Template ID
                    var emailTemplateId = root.GetProperty("forgotUserNameEmailTemplate").Value;

                    SendEmail(model.Email, "Health Republic Insurance - Username Recovery",
                              BuildEmail((int)emailTemplateId, dynamicText));

                    // Set the sucess flag to true and post back to the same page
                    TempData["ForgotUsernameIsSuccessful"] = true;
                    return(RedirectToCurrentUmbracoPage());
                }

                // The email has no member associated with it
                // Set the success flag to false and post back to the same page
                TempData["ForgotUsernameIsSuccessful"] = false;
                TempData["EmailNotFound"] = true;
                return(RedirectToCurrentUmbracoPage());
            }
            catch (Exception ex)
            {
                // Create an error message with sufficient info to contact the user
                string additionalInfo = "A user was trying to retrieve a username for the email " + model.Email + ".";
                // Add the error message to the log4net output
                log4net.GlobalContext.Properties["additionalInfo"] = additionalInfo;
                // Log the error
                logger.Error("User unable to retrieve forgotten user name.", ex);

                ModelState.AddModelError("forgotUserNameViewModel", ex.Message + "\n" + ex.InnerException.Message + "\n");
                // Set the success flag to false and post back to the same page
                TempData["ForgotUsernameIsSuccessful"] = false;
                return(RedirectToCurrentUmbracoPage());
            }
        }
コード例 #2
0
 public async Task <IActionResult> Post([FromBody] ForgotUserNameViewModel model)
 {
     return(new OkObjectResult(await _registerDesigneeService.ForgotUserName(model.Email)));
 }