Beispiel #1
0
        public static string GetFormatedAccountVerificationEmailTemplate(AccountVerificationData accountVerificationData, string path)
        {
            string text = File.ReadAllText(path);

            text = text.Replace("{siteName}", accountVerificationData.SiteName);
            text = text.Replace("{siteUrl}", accountVerificationData.SiteUrl);
            text = text.Replace("{title}", accountVerificationData.Title);
            text = text.Replace("{confirmUrl}", accountVerificationData.BaseUrl);
            text = text.Replace("{userName}", accountVerificationData.UserName);

            return(text);
        }
Beispiel #2
0
        public async Task <IActionResult> RegisterUser([FromBody] NewUserInputModel model)
        {
            // search role
            var role = _roleManager.FindByIdAsync(model.RoleID).Result;

            var user = new AppUser
            {
                UserName       = model.UserName,
                Email          = model.Email,
                OrganizationID = model.OrgID,
                EmailConfirmed = false,
                PhoneNumber    = model.PhoneNumber
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                // code for adding user to role
                await _userManager.AddToRoleAsync(user, role.Name);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                var uriBuilder = new UriBuilder(model.BaseUrl + "/confirm");
                var parameters = HttpUtility.ParseQueryString(string.Empty);
                parameters["userId"] = user.Id.ToString();
                parameters["code"]   = code;
                uriBuilder.Query     = parameters.ToString();

                Uri finalUrl = uriBuilder.Uri;

                AccountVerificationData verificationData = new AccountVerificationData
                {
                    UserName = model.UserName,
                    SiteName = _config.Value.SolutionName,
                    BaseUrl  = finalUrl.AbsoluteUri,
                    Title    = "APlus Account Verification",
                    SiteUrl  = _config.Value.BaseURL
                };

                await _emailSender.SendEmailAsync(model.Email, "APlus Account Verification", DataFormatManager.GetFormatedAccountVerificationEmailTemplate(verificationData, _hostingEnvironment.ContentRootPath + _templateParams.Value.AccountVerificationTemplate));

                return(Ok(new
                {
                    result = true,
                    message = ""
                }));
            }
            else
            {
                List <string> list = new List <string>();
                foreach (var error in result.Errors)
                {
                    list.Add(error.Description);
                }
                return(Ok(new
                {
                    result = false,
                    message = list
                }));
            }
        }