public async Task <ActionResult <UserDTO> > Register(RegisterDTO registerDTO)
        {
            if (await UserExists(registerDTO.Username))
            {
                return(BadRequest("Username alredy exists"));
            }
            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                UserName     = registerDTO.Username.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDTO.Password)),
                PasswordSalt = hmac.Key
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(new UserDTO {
                Username = user.UserName,
                Token = tokenService.CreateToken(user)
            });
        }
        public async Task <ActionResult <AppUser> > Register(RegisterDTO registerDTO)
        {
            if (await UserExists(registerDTO.userName))
            {
                return(BadRequest("Username is taken"));
            }


            var user = new AppUser {
                userName = registerDTO.userName.ToLower(),
                Name     = registerDTO.Name,
                Surname  = registerDTO.Surname,
                Age      = registerDTO.Age,
                Password = registerDTO.Password,
                Email    = registerDTO.Email
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(user);
        }
Example #3
0
        public async Task <ActionResult <UserDTO> > Register(RegisterDTO registerDto)
        {
            if (CheckEmailExistsAsync(registerDto.Email).Result.Value)
            {
                return(new BadRequestObjectResult(new ApiValidationErrorResponse
                {
                    Errors = new[] { "Email address is in use" }
                }));
            }

            if (CheckUserNameExistsAsync(registerDto.UserName).Result.Value)
            {
                return(new BadRequestObjectResult(new ApiValidationErrorResponse
                {
                    Errors = new[] { "UserName is in use" }
                }));
            }

            var user = new User
            {
                UserName = registerDto.UserName,
                Email    = registerDto.Email,
            };

            var result = await _userManager.CreateAsync(user, registerDto.Password);

            if (!result.Succeeded)
            {
                return(BadRequest(new ApiResponse(400)));
            }

            return(new UserDTO
            {
                Email = user.Email,
                Token = _tokenService.CreateToken(user),
                UserName = user.UserName
            });
        }
Example #4
0
        public async Task <ActionResult <UserDTO> > Register(RegisterDTO registerDto)
        {
            if (await _userManager.Users.AnyAsync(x => x.Email == registerDto.Email))
            {
                ModelState.AddModelError("email", "Email taken");
                return(ValidationProblem());
            }
            if (await _userManager.Users.AnyAsync(x => x.UserName == registerDto.Username))
            {
                ModelState.AddModelError("username", "Username taken");
                return(ValidationProblem());
            }

            var user = new AppUser
            {
                DisplayName = registerDto.DisplayName,
                Email       = registerDto.Email,
                UserName    = registerDto.Username
            };

            var result = await _userManager.CreateAsync(user, registerDto.Password);

            if (!result.Succeeded)
            {
                return(BadRequest("Problem registering user"));
            }

            var origin = Request.Headers["origin"];
            var token  = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            token = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(token));

            var verifyUrl = $"{origin}/account/verifyEmail?token={token}&email={user.Email}";
            var message   = $"<p>Please click the below link to verify your email address:</p><p><a href='{verifyUrl}'>Click to verify email</a></p>";

            return(Ok("Registration success - please verify email"));
        }
Example #5
0
        public async Task <ActionResult <UserTokenDTO> > Register(RegisterDTO resgister)
        {
            if (await UserExists(resgister.username))
            {
                return(BadRequest("username is taken"));
            }
            using var hmac = new HMACSHA512();
            var passwordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(resgister.password));
            var passwordSalt = hmac.Key;
            var user         = new AppUser()
            {
                UserName     = resgister.username,
                PasswordHash = passwordHash,
                PasswordSalt = passwordSalt
            };

            _context.User.Add(user);
            await _context.SaveChangesAsync();

            return(new UserTokenDTO {
                username = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
Example #6
0
        [HttpPost("register")]  //creating a user
        public async Task <ActionResult <UserDto> > Register(RegisterDTO registerDTO)
        {
            if (await UserExists(registerDTO.Username))
            {
                return(BadRequest("Username is taken"));                                       //Here we check first if the user exists
            }
            using var hmac = new HMACSHA512();

            var user = new AppUser
            {
                UserName     = registerDTO.Username.ToLower(), //This causes all our usernames to be stored in lowercase
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(registerDTO.Password)),
                PasswordSalt = hmac.Key
            };

            _context.Users.Add(user);          //saying to EF add this to our users table..not really adding to the database but tracking
            await _context.SaveChangesAsync(); //This is where we do call our database and save our user into user table

            return(new UserDto
            {
                Username = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
 public IActionResult Register([FromBody] RegisterDTO data)
 {
     _userService.Register(data);
     return(Ok("succesfull registration"));
 }
 public async Task <bool> userExists(RegisterDTO registerDTO)
 {
     return(await _context.Users.AnyAsync(u => u.UserName == registerDTO.username.ToLower()));
 }