public void Authenticate(IRestClient client, IRestRequest request)
        {
            string accessToken = _authenticationContext.AcquireTokenAsync(_resource, _clientCredential)
                                 .Result
                                 .AccessToken;

            JwtAuthenticator _jwtAuthenticator = new JwtAuthenticator(accessToken);

            _jwtAuthenticator.Authenticate(client, request);
        }
        public void ShouldNotAuthenticate()
        {
            // Create a valid token.
            const string validSecret   = "a valid secret that needs to be at least 16 characters long";
            const string invalidSecret = "an invalid secret that needs to be at least 16 characters long";
            const string issuer        = "example.com";
            const string audience      = "example.com";
            const string username      = "******";

            var securityKey        = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(validSecret));
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256, SecurityAlgorithms.Sha256Digest);

            var claims = new[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, username),
            };

            var token = new JwtSecurityToken(
                issuer: issuer,
                audience: audience,
                claims: claims,
                expires: DateTime.UtcNow.AddSeconds(1), // Expired!
                notBefore: DateTime.UtcNow,
                signingCredentials: signingCredentials);

            var tokenHandler = new JwtSecurityTokenHandler();
            var encodedToken = tokenHandler.WriteToken(token);

            using (var stream = new MemoryStream())
            {
                var writer = new DataWriter(stream);
                writer.Write($"Token={encodedToken}");

                stream.Seek(0, SeekOrigin.Begin);
                var authenticator = new JwtAuthenticator(new[] { invalidSecret });
                Assert.ThrowsException <SecurityException>(() =>
                {
                    authenticator.Authenticate(stream);
                });
            }
        }