public void VerifyPasswordHash_Test(string inputPassword, bool expectedResult)
        {
            // Arrange
            const string rightPassword = "******";
            var          rightSalt     = UTF8.GetBytes("SomePasswordSalt");
            var          passwordHash  = new HMACSHA512(rightSalt).ComputeHash(UTF8.GetBytes(rightPassword));

            // Act
            var result = PasswordHelpers.VerifyPasswordHash(inputPassword, passwordHash, rightSalt);

            // Assert
            Assert.Equal(expectedResult, result);
        }
示例#2
0
        public async Task Update_ValidParams_DataUpdated()
        {
            CreateEntities(out List <User> users);
            var    userService = CreateService(users);
            var    user        = users.SingleOrDefault(x => x.Id == 101);
            string newPassword = "******";

            await userService.Update(user, newPassword);

            var result = users.SingleOrDefault(x => x.Id == 101);

            Assert.True(PasswordHelpers.VerifyPasswordHash("newPassword101", result.PasswordHash, result.PasswordSalt));
        }
示例#3
0
        public Room Authenticate(int id, string password)
        {
            var room = _context.Rooms
                       .Include(x => x.RoomUsers)
                       .SingleOrDefault(x => x.Id == id);

            if (room == null)
            {
                throw new ApplicationException(Properties.resultMessages.RoomNotFound);
            }

            if (!PasswordHelpers.VerifyPasswordHash(password, room.PasswordHash, room.PasswordSalt))
            {
                throw new ApplicationException(Properties.resultMessages.CredentialsInvalid);
            }

            return(room);
        }
        public User AuthenticateUser(UserLoginDto userLoginDto)
        {
            var user = _context.Users.SingleOrDefault(x => x.Username == userLoginDto.Username);

            // check if the username exists
            if (user == null)
            {
                return(null);
            }

            // check if the password is correct
            if (!PasswordHelpers.VerifyPasswordHash(userLoginDto.Password, user.PasswordHash, user.PasswordSalt))
            {
                return(null);
            }

            return(user);
        }
示例#5
0
        public async Task <User> Authenticate(string username, string password)
        {
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                throw new ApplicationException(Properties.resultMessages.CredentialsInvalid);
            }

            var user = await _context.Users.SingleOrDefaultAsync(x => x.Username == username);

            if (user == null)
            {
                throw new ApplicationException(Properties.resultMessages.CredentialsInvalid);
            }

            if (!PasswordHelpers.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
            {
                throw new ApplicationException(Properties.resultMessages.CredentialsInvalid);
            }

            return(user);
        }
        public void Update_ValidParams_DataUpdated()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var rooms = new List <Room>
            {
                new Room {
                    Id = 1, Title = "Room1", PasswordHash = passwordHash, PasswordSalt = passwordSalt
                }
            };
            var  roomService = CreateService(rooms);
            Room roomParam   = new Room {
                Id = 1, Title = "NewRoom1"
            };
            string password = "******";

            roomService.Update(roomParam, password);

            var room = roomService.GetById(1);

            Assert.Equal("NewRoom1", room.Title);
            Assert.True(PasswordHelpers.VerifyPasswordHash("newPassword123", room.PasswordHash, room.PasswordSalt));
        }
示例#7
0
        public void Update_ValidParams_DataUpdated()
        {
            PasswordHelpers.CreatePasswordHash("password123", out byte[] passwordHash, out byte[] passwordSalt);
            var users = new List <User>
            {
                new User {
                    Id = 1, Username = "******", PasswordHash = passwordHash, PasswordSalt = passwordSalt
                }
            };
            var  userService = CreateService(users);
            User userParam   = new User {
                Id = 1, Username = "******"
            };
            string password = "******";

            userService.Update(userParam, password);

            var user = userService.GetById(1);

            Assert.Equal("NewUser1", user.Username);
            Assert.True(PasswordHelpers.VerifyPasswordHash("newPassword123", user.PasswordHash, user.PasswordSalt));
        }