Beispiel #1
0
        public async Task <ActionResult <UserDto> > RegisterUser([FromBody] UserDto userDto, string password)
        {
            try
            {
                if (string.IsNullOrEmpty(userDto.Name))
                {
                    throw new ArgumentNullException(nameof(userDto.Name));
                }

                if (string.IsNullOrEmpty(password))
                {
                    throw new ArgumentNullException(nameof(password), "cannot be null or empty");
                }
                if (await CheckIfUsernameTaken(userDto.Name))
                {
                    return(StatusCode(406));
                }

                var salt = _salter.CreateSalt();
                _saltAgorithm ??= new SaltAgorithm();
                BasicAuthenticationHandler.Hashing hashdelegate = _saltAgorithm.Hash;
                var hash = _salter.GenerateSaltedHash(password, salt, hashdelegate);

                var user = _mapper.Map <User>(userDto);

                await _context.Users.AddAsync(_mapper.Map <User>(user));

                await _context.SaveChangesAsync();

                var userPassword = new Password {
                    Hash = hash, UserId = user.Id, Salt = salt
                };

                await _context.Passwords.AddAsync(userPassword);

                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetUser", new { id = user.Id }, _mapper.Map <UserDto>(user)));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest());
            }
        }
 public byte[] GenerateSaltedHash(string plainText, byte[] salt, BasicAuthenticationHandler.Hashing func)
 {
     return(func(plainText, salt));
 }