예제 #1
0
        public async Task <AuthResponse> RegisterAsync(string email, string password)
        {
            var(hash, salt) = _stringHasherService.HashStringWithHmacAndSalt(password);

            var user = new User
            {
                Email        = email,
                PasswordHash = hash,
                PasswordSalt = salt
            };

            await _userService.CreateUserAsync(user);

            await _userService.AddToRoleAsync(user, ApiRoles.User);

            await _emailSender.SendEmailAsync(user.Email, "Welcome to RestBnb!", "Thank you for signing up.");

            return(await _authenticationServiceHelper.GetAuthenticationResultAsync(user));
        }
        public async Task <IActionResult> Register([FromBody] RegisterUserDto registerDto)
        {
            if (!ModelState.IsValid)
            {
                return(StatusCodeAndDtoWrapper.BuilBadRequest(ModelState));
            }
            var user = new ApplicationUser
            {
                FirstName = registerDto.FirstName,
                LastName  = registerDto.LastName,
                UserName  = registerDto.Username,
                Email     = registerDto.Email
            };

            var result = await _usersService.Create(user, registerDto.Password);

            if (result.Succeeded)
            {
                if (result.Succeeded)
                {
                    result = await _usersService.AddToRoleAsync(user, "ROLE_USER");

                    if (result.Succeeded)
                    {
                        return(StatusCodeAndDtoWrapper.BuildSuccess("Registered successfully"));
                    }
                    else
                    {
                        return(StatusCodeAndDtoWrapper.BuildBadRequest(result.Errors));
                    }
                }
                else
                {
                    return(StatusCodeAndDtoWrapper.BuildBadRequest(result.Errors));
                }
            }
            else
            {
                return(StatusCodeAndDtoWrapper.BuildBadRequest(result.Errors));
            }
        }