From c5bfdb7053fbf02bb4efff6997b5e3da772e997a Mon Sep 17 00:00:00 2001 From: maliming <6908465+maliming@users.noreply.github.com> Date: Fri, 17 Jul 2020 17:03:02 +0800 Subject: [PATCH] Add CacheAbsoluteExpiration to IdentityClientConfiguration. --- .../IdentityClientConfiguration.cs | 25 ++++++++++++---- .../IdentityModelAuthenticationService.cs | 29 +++++++------------ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs index 17040ff1db..d831c28808 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using IdentityModel; namespace Volo.Abp.IdentityModel @@ -81,21 +82,32 @@ namespace Volo.Abp.IdentityModel get => this.GetOrDefault(nameof(RequireHttps))?.To() ?? true; set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant(); } - + + /// + /// Cache absolute expiration + /// Default: 30 minutes. + /// + public double CacheAbsoluteExpiration + { + get => this.GetOrDefault(nameof(CacheAbsoluteExpiration ))?.To() ?? 60 * 30; + set => this[nameof(CacheAbsoluteExpiration)] = value.ToString(CultureInfo.InvariantCulture); + } + public IdentityClientConfiguration() { - + } public IdentityClientConfiguration( string authority, string scope, - string clientId, - string clientSecret, + string clientId, + string clientSecret, string grantType = OidcConstants.GrantTypes.ClientCredentials, string userName = null, string userPassword = null, - bool requireHttps = true) + bool requireHttps = true, + double cacheAbsoluteExpiration = 60 * 30) { this[nameof(Authority)] = authority; this[nameof(Scope)] = scope; @@ -105,6 +117,7 @@ namespace Volo.Abp.IdentityModel this[nameof(UserName)] = userName; this[nameof(UserPassword)] = userPassword; this[nameof(RequireHttps)] = requireHttps.ToString().ToLowerInvariant(); + this[nameof(CacheAbsoluteExpiration)] = cacheAbsoluteExpiration.ToString(CultureInfo.InvariantCulture); } } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs index 5d22d0c7a5..0fb4c32648 100644 --- a/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs +++ b/framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs @@ -78,13 +78,11 @@ namespace Volo.Abp.IdentityModel public virtual async Task GetAccessTokenAsync(IdentityClientConfiguration configuration) { - var tokenEndpoint = await GetTokenEndpoint(configuration); - var cacheKey = CalculateTokenCacheKey(configuration); var tokenCacheItem = await TokenCache.GetAsync(cacheKey); if (tokenCacheItem == null) { - var tokenResponse = await GetTokenResponse(tokenEndpoint, configuration); + var tokenResponse = await GetTokenResponse(configuration); if (tokenResponse.IsError) { @@ -99,14 +97,12 @@ namespace Volo.Abp.IdentityModel throw new AbpException(withoutInnerException[0]); } - await TokenCache.SetAsync(cacheKey, new IdentityModelTokenCacheItem(tokenResponse.AccessToken), - new DistributedCacheEntryOptions() + tokenCacheItem = new IdentityModelTokenCacheItem(tokenResponse.AccessToken); + await TokenCache.SetAsync(cacheKey, tokenCacheItem, + new DistributedCacheEntryOptions { - //Subtract 10 seconds of network request time. - AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(tokenResponse.ExpiresIn - 10) + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.CacheAbsoluteExpiration) }); - - return tokenResponse.AccessToken; } return tokenCacheItem.AccessToken; @@ -148,15 +144,14 @@ namespace Volo.Abp.IdentityModel await DiscoveryDocumentCache.SetAsync(tokenEndpointUrlCacheKey, discoveryDocumentCacheItem, new DistributedCacheEntryOptions { - SlidingExpiration = TimeSpan.FromMinutes(30) + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.CacheAbsoluteExpiration) }); } return discoveryDocumentCacheItem.TokenEndpoint; } - protected virtual async Task GetDiscoveryResponse( - IdentityClientConfiguration configuration) + protected virtual async Task GetDiscoveryResponse(IdentityClientConfiguration configuration) { using (var httpClient = HttpClientFactory.CreateClient(HttpClientName)) { @@ -173,10 +168,10 @@ namespace Volo.Abp.IdentityModel } } - protected virtual async Task GetTokenResponse( - string tokenEndpoint, - IdentityClientConfiguration configuration) + protected virtual async Task GetTokenResponse(IdentityClientConfiguration configuration) { + var tokenEndpoint = await GetTokenEndpoint(configuration); + using (var httpClient = HttpClientFactory.CreateClient(HttpClientName)) { AddHeaders(httpClient); @@ -217,9 +212,7 @@ namespace Volo.Abp.IdentityModel return Task.FromResult(request); } - protected virtual Task CreateClientCredentialsTokenRequestAsync( - string tokenEndpoint, - IdentityClientConfiguration configuration) + protected virtual Task CreateClientCredentialsTokenRequestAsync(string tokenEndpoint, IdentityClientConfiguration configuration) { var request = new ClientCredentialsTokenRequest {