Ejemplo n.º 1
0
        public async Task <ActionResult <UserDto> > Register(AccountDtos account)
        {
            if (await UserExists(account.UserName))
            {
                return(BadRequest("Username is Taken"));
            }
            using var hmac = new HMACSHA512();
            var user = new AppUser
            {
                UserName     = account.UserName.ToLower(),
                PasswordHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(account.Password)),
                PasswordSalt = hmac.Key
            };

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

            return(new UserDto {
                UserName = user.UserName,
                Token = _tokenService.CreateToken(user)
            });
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Register(AccountDtos accountDtos)
        {
            accountDtos.Login = accountDtos.Login.ToLower();

            if (await _repo.UserExists(accountDtos.Login))
            {
                return(BadRequest("Username already exists"));
            }

            var accountToCreate = new Account
            {
                Login       = accountDtos.Login,
                Birthdate   = accountDtos.Birthdate,
                Email       = accountDtos.Email,
                LiveAddress = accountDtos.Email,
                PhoneNumber = accountDtos.PhoneNumber,
                Name        = accountDtos.Name,
                Surname     = accountDtos.Surname
            };

            var createdAccount = await _repo.Register(accountToCreate, accountDtos.Password);

            return(StatusCode(201));
        }