public async Task <OperationDetails> CreateUserAsync(RegistrationUserDTO userDTO)
        {
            if (!userDTO.IsValid())
            {
                return(userDTO.GetValidateError());
            }
            var dbUser = await dataStore.UserProfiles.GetByNameAsync(userDTO.UserName);

            if (dbUser != null)
            {
                return(new OperationDetails(false, new string[] { "This username used yet." }));
            }
            var applicationUser = userDTO.GetApplicationUser();
            var result          = await userManager.Create(applicationUser, userDTO.Password);

            if (!result.IsSuccess)
            {
                return(new OperationDetails(false, result.ErrorList.Select(x => x.Description)));
            }
            var userProfile = userDTO.GetUserProfile();

            userProfile.IdentityUserId = (await userManager.GetUserByEmailAsync(applicationUser.Email)).Id;
            var createdProfile = await dataStore.UserProfiles.CreateAsync(userProfile);

            if (createdProfile == null)
            {
                var errorUser = await userManager.GetUserByEmailAsync(applicationUser.Username);

                var res = await userManager.DeleteUserAsync(errorUser);

                return(new OperationDetails(false, new string[] { "Some error, try another data." }));
            }
            return(new OperationDetails(true));
        }
Beispiel #2
0
        public async Task <IActionResult> DeleteUser(string id)
        {
            if (!(await _authorizationService.AuthorizeAsync(User, id, AccountManagementOperations.Delete)).Succeeded)
            {
                return(new ChallengeResult());
            }

            User appUser = await _accountManager.GetUserByIdAsync(id);

            if (appUser == null)
            {
                return(NotFound(id));
            }

            if (!await _accountManager.TestCanDeleteUserAsync(id))
            {
                return(BadRequest("User cannot be deleted. Delete all orders associated with this user and try again"));
            }

            UserViewModel userVM = await GetUserViewModelHelper(appUser.Id);

            (bool Succeeded, string[] Errors)result = await _accountManager.DeleteUserAsync(appUser);

            if (!result.Succeeded)
            {
                throw new Exception("The following errors occurred whilst deleting user: "******", ", result.Errors));
            }

            return(Ok(userVM));
        }
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            bool result = await _identityManager.DeleteUserAsync(id);

            return(RedirectToAction(nameof(SearchUsers)));
        }