Beispiel #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                var user = new Extensions.Security.User
                {
                    UserName           = Input.UserName,
                    NormalizedUserName = _userManager.NormalizeName(Input.UserName),
                    NickName           = Input.NickName ?? Input.UserName,
                    PasswordHash       = Input.Password,
                    Email                = Input.Email,
                    NormalizedEmail      = _userManager.NormalizeEmail(Input.Email),
                    PhoneNumber          = Input.PhoneNumber,
                    EmailConfirmed       = !string.IsNullOrEmpty(Input.Email) && !Settings.RequireConfirmedEmail,
                    PhoneNumberConfirmed = !string.IsNullOrEmpty(Input.PhoneNumber) && !Settings.RequireConfirmedPhoneNumber,
                    LockoutEnabled       = true,
                    ParentId             = UserId
                };
                user.TwoFactorEnabled = (!string.IsNullOrEmpty(Input.Email) || !string.IsNullOrEmpty(Input.PhoneNumber)) && Settings.RequiredTwoFactorEnabled;
                var result = await _userManager.IsDuplicatedAsync(user);

                if (!result.Succeeded)
                {
                    return(Error(result.ToErrorString()));
                }
                result = await _userManager.CreateAsync(user, user.PasswordHash);

                if (result.Succeeded)
                {
                    Log($"添加了账户“{Input.UserName}”的信息!");
                    return(Success("你已经成功添加了账户!"));
                }
                return(Error(result.ToErrorString()));
            }
            return(Error());
        }