Exemple #1
0
        public static Task <OidcDataManager> CreateCachedContext(KeycloakAuthenticationOptions options,
                                                                 bool preload = true)
        {
            Task <OidcDataManager> preloadTask = null;
            var newContext = new OidcDataManager(options);

            if (preload)
            {
                preloadTask = newContext.ValidateCachedContextAsync();
            }
            HttpRuntime.Cache.Insert(options.AuthenticationType + CachedContextPostfix, newContext, null,
                                     Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
            return(preload ? preloadTask : Task.FromResult(newContext));
        }
Exemple #2
0
        public SecurityToken ValidateToken(string jwt, KeycloakAuthenticationOptions options)
        {
            var uriManager = OidcDataManager.GetCachedContext(options);
            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateLifetime         = true,
                RequireExpirationTime    = true,
                ValidateIssuer           = !options.DisableIssuerValidation,
                ValidateAudience         = !options.DisableAudienceValidation,
                ValidateIssuerSigningKey = !options.DisableTokenSignatureValidation,
                RequireSignedTokens      = !options.AllowUnsignedTokens,
                ValidIssuer    = uriManager.GetIssuer(),
                ClockSkew      = options.TokenClockSkew,
                ValidAudiences = new List <string> {
                    "null", options.ClientId
                },
                IssuerSigningTokens = uriManager.GetJsonWebKeys().GetSigningTokens(),
                AuthenticationType  = options.AuthenticationType // Not used
            };

            return(ValidateToken(jwt, tokenValidationParameters));
        }
Exemple #3
0
        public static async Task <SecurityToken> ValidateTokenRemote(string jwt, KeycloakAuthenticationOptions options)
        {
            // This should really only be used on access tokens...
            var uriManager = OidcDataManager.GetCachedContext(options);
            var uri        = new Uri(uriManager.TokenValidationEndpoint, "?access_token=" + jwt);

            try
            {
                var client   = new HttpClient();
                var response = await client.GetAsync(uri);

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception();
                }
                return(new JwtSecurityToken(jwt)); // TODO: Get this from returned JSON
            }
            catch (Exception)
            {
                throw new SecurityTokenValidationException("Remote Token Validation Failed");
            }
        }
 public static Task<OidcDataManager> CreateCachedContext(KeycloakAuthenticationOptions options,
     bool preload = true)
 {
     Task<OidcDataManager> preloadTask = null;
     var newContext = new OidcDataManager(options);
     if (preload) preloadTask = newContext.ValidateCachedContextAsync();
     HttpRuntime.Cache.Insert(options.AuthenticationType + CachedContextPostfix, newContext, null,
         Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
     return preload ? preloadTask : Task.FromResult(newContext);
 }