public static IAppBuilder UseJsonWebToken(this IAppBuilder app, string issuer, string audience, string signingKey, string type = null, OAuthBearerAuthenticationProvider location = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            var options = new JwtBearerAuthenticationOptions
            {
                AllowedAudiences = new[] { audience },
                IssuerSecurityTokenProviders = new[] 
                    {
                        new SymmetricKeyIssuerSecurityTokenProvider(
                            issuer,
                            signingKey)
                    }
            };

            if (!string.IsNullOrEmpty(type))
            {
                options.AuthenticationType = type;
            }

            if (location != null)
            {
                options.Provider = location;
            }

            app.UseJwtBearerAuthentication(options);
            
            return app;
        }
        public static IAppBuilder UseJsonWebToken(this IAppBuilder app, string issuer, string audience, X509Certificate2 signingKey, OAuthBearerAuthenticationProvider location = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            var options = new JwtBearerAuthenticationOptions
            {
                AllowedAudiences = new[] { audience },
                IssuerSecurityTokenProviders = new[] 
                    {
                        new X509CertificateSecurityTokenProvider(
                            issuer,
                            signingKey)
                    }
            };

            if (location != null)
            {
                options.Provider = location;
            }

            app.UseJwtBearerAuthentication(options);

            return app;
        }
        public async Task Valid_Token_With_ValidatingIdentity_Deny_Access()
        {
            var provider = new OAuthBearerAuthenticationProvider
            {
                OnValidateIdentity = c =>
                {
                    c.Rejected();
                    
                    return Task.FromResult(0);
                }
            };

            _options.TokenProvider = provider;

            var client = PipelineFactory.CreateHttpClient(_options);
            var token = TokenFactory.CreateTokenString(TokenFactory.CreateToken());
            client.SetBearerToken(token);

            var result = await client.GetAsync("http://test");
            result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
        }
        public async Task Token_From_QueryString()
        {
            var provider = new OAuthBearerAuthenticationProvider
            {
                OnRequestToken = c =>
                {
                    var qs = c.OwinContext.Request.Query;
                    c.Token = qs.Get("access_token");

                    return Task.FromResult(0);
                }
            };

            _options.TokenProvider = provider;

            var client = PipelineFactory.CreateHttpClient(_options);
            var token = TokenFactory.CreateTokenString(TokenFactory.CreateToken());

            var result = await client.GetAsync("http://test?access_token=" + token);
            result.StatusCode.Should().Be(HttpStatusCode.OK);

        }