コード例 #1
0
        public async Task <UserRegisteredDto> RegisterUserAsync(UserRegisterDto userRegisterDto)
        {
            var existingUser = await this._userManager.FindByEmailAsync(userRegisterDto.Email);

            if (existingUser != null)
            {
                return(new UserRegisteredDto
                {
                    Succeeded = false,
                    Error = AccountServiceConstants.UserWithMailExist,
                });
            }

            var newUser     = _mapper.Map <User>(userRegisterDto);
            var createdUser = await this._userManager.CreateAsync(newUser, userRegisterDto.Password);

            var returnedUser = new UserRegisteredDto
            {
                Succeeded = createdUser.Succeeded
            };

            if (!createdUser.Succeeded)
            {
                returnedUser.Error = string.Join(" ", createdUser.Errors.Select(x => x.Description));
                returnedUser.User  = _mapper.Map <UserDto>(newUser);
                return(returnedUser);
            }

            await _userManager.AddToRoleAsync(newUser, Roles.User);

            returnedUser.User       = _mapper.Map <UserDto>(newUser);
            returnedUser.User.Roles = await _userManager.GetRolesAsync(newUser);

            return(returnedUser);
        }
コード例 #2
0
        public async Task <ActionResult <UserDto> > RegisteredAccount([FromBody] UserRegisteredDto userRegisteredDto)
        {
            var user = mapper.Map <User>(userRegisteredDto);

            var result = await userService.RegisteredUser(user);

            var returnDto = mapper.Map <UserDto>(user);

            if (result == true)
            {
                await userService.SaveAsync();

                return(CreatedAtRoute(nameof(GetUserInfo), new { userId = user.Id }, returnDto));
            }

            return(BadRequest(returnDto));
        }
コード例 #3
0
        public bool TryRegisterUser(UserRegisterDto userRegisterDto, out UserRegisteredDto userRegisteredDto)
        {
            userRegisteredDto = null;

            if (!IsUserExist(userRegisterDto))
            {
                var user = mapper.Map <User>(userRegisterDto);

                userRepository.Insert(user);
                unitOfWork.Save();

                userRegisteredDto = mapper.Map <UserRegisteredDto>(user);
                return(true);
            }

            return(false);
        }