Exemplo n.º 1
0
        public async Task CreateRefreshTokenUnderUserByIdAsync(string signature, int userId,
                                                               string ipAddress, string userAgent)
        {
            var refreshToken = new RefreshToken
            {
                Signature = signature,
                IsExpired = false,
                IpAddress = ipAddress,
                UserAgent = userAgent,
                UserId    = userId
            };

            _context.RefreshTokens.Add(refreshToken);
            await _context.SaveChangesAsync();

            return;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new user.
        /// </summary>
        /// <param name="newUser">The new user entity to create.</param>
        /// <param name="password">The password for the user.</param>
        /// <exception cref="UserServiceException">Throws when missing or invalid/reserved values are found.</exception>
        /// <returns>The created user.</returns>
        public async Task <User> CreateAsync(User newUser, string password)
        {
            // ensure username is sent
            if (string.IsNullOrWhiteSpace(newUser.Username))
            {
                throw new UserServiceException("Username is required");
            }

            // ensure password is sent
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new UserServiceException("Password is required");
            }

            // ensure email is sent
            if (string.IsNullOrWhiteSpace(newUser.Email))
            {
                throw new UserServiceException("Email is required");
            }

            // ensure username doesn't already exist
            if (await _context.Users.AnyAsync(x => x.Username == newUser.Username))
            {
                throw new UserServiceException($"Username '{newUser.Username}' is taken");
            }

            // ensure email doesn't already exist
            if (await _context.Users.AnyAsync(x => x.Email == newUser.Email))
            {
                throw new UserServiceException($"Email '{newUser.Email}' is taken");
            }

            // generate hash and salt from the given password
            HashUtils.GenerateHashAndSaltFromString(password, out byte[] hash, out byte[] salt);
            newUser.PasswordHash = hash;
            newUser.PasswordSalt = salt;

            // standard user by default
            newUser.IsAdmin = false;

            // add and save new user
            _context.Users.Add(newUser);
            await _context.SaveChangesAsync();

            return(newUser);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <SylvreBlockResponseDto> > CreateSylvreBlock(SylvreBlockDto newSylvreBlock)
        {
            int userId = int.Parse(User.Identity.Name);

            var entity = GetSylvreBlockEntityFromDto(newSylvreBlock, userId);

            _context.SylvreBlocks.Add(entity);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("CreateSylvreBlock", new { id = entity.Id },
                                   GetSylvreBlockResponseDtoFromEntity(entity)));
        }