public void TokenExpirationTest()
        {
            byte[] keyForHmacSha256 = new byte[64];

            var randomGen = RandomNumberGenerator.Create();
            randomGen.GetBytes(keyForHmacSha256);

            var securityKey = new SymmetricSecurityKey(keyForHmacSha256);
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            var options = new TokenProviderOptions
            {
                Issuer = "InColUn",
                Audience = "All",
                SigningCredentials = signingCredentials,
                Expiration = TimeSpan.FromMilliseconds(1000)
            };

            var provider = new TokenProvider(options);

            var token = provider.GenerateUserToken(1, "John Dwo");

            var id = provider.ValidateToken(token.access_token);

            Assert.NotNull(id);
            Assert.Equal(1, id.Value);
        }
 /// <summary>
 /// Generates new instance of TokenProvider using provided token options
 /// If signing credentials are not provided - default signing credentials will be used
 /// </summary>
 /// <param name="options"></param>
 public TokenProvider(TokenProviderOptions options)
 {
     this.options = options;
 }
        private static void ThrowIfInvalidOptions(TokenProviderOptions options)
        {
            if (string.IsNullOrEmpty(options.Issuer))
            {
                throw new ArgumentNullException(nameof(TokenProviderOptions.Issuer));
            }

            if (string.IsNullOrEmpty(options.Audience))
            {
                throw new ArgumentNullException(nameof(TokenProviderOptions.Audience));
            }

            if (options.Expiration == TimeSpan.Zero)
            {
                throw new ArgumentException("Must be a non-zero TimeSpan.", nameof(TokenProviderOptions.Expiration));
            }
        }
Exemple #4
0
        private void ConfigureToken(IServiceCollection services)
        {
            byte[] keyForHmacSha256 = new byte[64];

            var randomGen = RandomNumberGenerator.Create();
            randomGen.GetBytes(keyForHmacSha256);

            var securityKey = new SymmetricSecurityKey(keyForHmacSha256);
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            var options = new TokenProviderOptions
            {
                Issuer = "InColUn",
                Audience = "All",
                SigningCredentials = signingCredentials,
                Expiration = TimeSpan.FromMilliseconds(1000)
            };

            var tokenProvider = new TokenProvider(options);

            services.AddSingleton(tokenProvider);
        }
        public void TokenPerformanceTest()
        {
            byte[] keyForHmacSha256 = new byte[64];
            var randomGen = RandomNumberGenerator.Create();
            randomGen.GetBytes(keyForHmacSha256);

            var securityKey = new SymmetricSecurityKey(keyForHmacSha256);
            var signingCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            var options = new TokenProviderOptions
            {
                Issuer = "InColUn",
                Audience = "All",
                SigningCredentials = signingCredentials
            };

            var provider = new TokenProvider(options);

            for (int i = 0; i < 128 * 2; i++)
            {
                var token = provider.GenerateUserToken(1, "John Dwo");
            }

            Stopwatch stopwatch = Stopwatch.StartNew();
            int limit = 128 * 1024;
            for (int i = 0; i < limit; i++)
            {
                var token = provider.GenerateUserToken(i, "John Dwo");
            }
            stopwatch.Stop();

            var output = string.Format("{0} tokens/sec", 1000.0 * limit / stopwatch.Elapsed.TotalMilliseconds);
            Console.WriteLine(output);
        }