Beispiel #1
0
    public virtual async Task <string> GetAccessTokenAsync(IdentityClientConfiguration configuration)
    {
        var cacheKey       = CalculateTokenCacheKey(configuration);
        var tokenCacheItem = await TokenCache.GetAsync(cacheKey);

        if (tokenCacheItem == null)
        {
            var tokenResponse = await GetTokenResponse(configuration);

            if (tokenResponse.IsError)
            {
                if (tokenResponse.ErrorDescription != null)
                {
                    throw new AbpException($"Could not get token from the OpenId Connect server! ErrorType: {tokenResponse.ErrorType}. " +
                                           $"Error: {tokenResponse.Error}. ErrorDescription: {tokenResponse.ErrorDescription}. HttpStatusCode: {tokenResponse.HttpStatusCode}");
                }

                var rawError = tokenResponse.Raw;
                var withoutInnerException = rawError.Split(new string[] { "<eof/>" }, StringSplitOptions.RemoveEmptyEntries);
                throw new AbpException(withoutInnerException[0]);
            }

            tokenCacheItem = new IdentityModelTokenCacheItem(tokenResponse.AccessToken);
            await TokenCache.SetAsync(cacheKey, tokenCacheItem,
                                      new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.CacheAbsoluteExpiration)
            });
        }

        return(tokenCacheItem.AccessToken);
    }
Beispiel #2
0
 protected virtual string CalculateTokenCacheKey(IdentityClientConfiguration configuration)
 {
     return(IdentityModelTokenCacheItem.CalculateCacheKey(configuration));
 }