public virtual async Task <ActionResult> NewUser(
            Guid?siteId)
        {
            var selectedSite = await SiteManager.GetSiteForDataOperations(siteId);

            // only server admin site can edit other sites settings
            if (selectedSite.Id != SiteManager.CurrentSite.Id)
            {
                ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["{0} - New User"], selectedSite.SiteName);
            }
            else
            {
                ViewData["Title"] = StringLocalizer["New User"];
            }

            var model = new NewUserViewModel
            {
                SiteId = selectedSite.Id
            };

            var viewName = await CustomUserInfo.GetNewUserViewName(UserManager.Site, HttpContext);

            await CustomUserInfo.HandleNewUserGet(
                UserManager.Site,
                model,
                HttpContext,
                ViewData);

            return(View(viewName, model));
        }
        public virtual async Task <IActionResult> NewUser(NewUserViewModel model)
        {
            var selectedSite = await SiteManager.GetSiteForDataOperations(model.SiteId);

            // only server admin site can edit other sites settings
            if (selectedSite.Id != SiteManager.CurrentSite.Id)
            {
                ViewData["Title"] = string.Format(CultureInfo.CurrentUICulture, StringLocalizer["{0} - New User"], selectedSite.SiteName);
            }
            else
            {
                ViewData["Title"] = StringLocalizer["New User"];
            }

            bool isValid = ModelState.IsValid;

            bool userNameAvailable = await UserManager.LoginIsAvailable(model.UserId, model.Username);

            if (!userNameAvailable)
            {
                ModelState.AddModelError("usernameerror", StringLocalizer["Username is already in use"]);
                isValid = false;
            }

            bool customDataIsValid = await CustomUserInfo.HandleNewUserValidation(
                selectedSite,
                model,
                HttpContext,
                ViewData,
                ModelState);


            if (isValid && customDataIsValid)
            {
                var user = new SiteUser()
                {
                    SiteId      = selectedSite.Id,
                    UserName    = model.Username,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName
                };

                if (model.DateOfBirth.HasValue)
                {
                    user.DateOfBirth = model.DateOfBirth.Value;
                }

                await CustomUserInfo.ProcessUserBeforeCreate(user, HttpContext);

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

                if (result.Succeeded)
                {
                    await CustomUserInfo.HandleNewUserPostSuccess(
                        selectedSite,
                        user,
                        model,
                        HttpContext);

                    if (model.MustChangePwd)
                    {
                        user.MustChangePwd = true;
                        await UserManager.UpdateAsync(user);
                    }

                    this.AlertSuccess(string.Format(StringLocalizer["user account for {0} was successfully created."],
                                                    user.DisplayName), true);

                    return(RedirectToAction("Index", "UserAdmin", new { siteId = selectedSite.Id }));
                }
                AddErrors(result);
            }

            var viewName = await CustomUserInfo.GetNewUserViewName(UserManager.Site, HttpContext);

            // If we got this far, something failed, redisplay form
            return(View(viewName, model));
        }