Browse Source

Add CacheAbsoluteExpiration to IdentityClientConfiguration.

pull/4742/head
maliming 6 years ago
parent
commit
c5bfdb7053
  1. 25
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs
  2. 29
      framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

25
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityClientConfiguration.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using IdentityModel; using IdentityModel;
namespace Volo.Abp.IdentityModel namespace Volo.Abp.IdentityModel
@ -81,21 +82,32 @@ namespace Volo.Abp.IdentityModel
get => this.GetOrDefault(nameof(RequireHttps))?.To<bool>() ?? true; get => this.GetOrDefault(nameof(RequireHttps))?.To<bool>() ?? true;
set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant(); set => this[nameof(RequireHttps)] = value.ToString().ToLowerInvariant();
} }
/// <summary>
/// Cache absolute expiration
/// Default: 30 minutes.
/// </summary>
public double CacheAbsoluteExpiration
{
get => this.GetOrDefault(nameof(CacheAbsoluteExpiration ))?.To<double>() ?? 60 * 30;
set => this[nameof(CacheAbsoluteExpiration)] = value.ToString(CultureInfo.InvariantCulture);
}
public IdentityClientConfiguration() public IdentityClientConfiguration()
{ {
} }
public IdentityClientConfiguration( public IdentityClientConfiguration(
string authority, string authority,
string scope, string scope,
string clientId, string clientId,
string clientSecret, string clientSecret,
string grantType = OidcConstants.GrantTypes.ClientCredentials, string grantType = OidcConstants.GrantTypes.ClientCredentials,
string userName = null, string userName = null,
string userPassword = null, string userPassword = null,
bool requireHttps = true) bool requireHttps = true,
double cacheAbsoluteExpiration = 60 * 30)
{ {
this[nameof(Authority)] = authority; this[nameof(Authority)] = authority;
this[nameof(Scope)] = scope; this[nameof(Scope)] = scope;
@ -105,6 +117,7 @@ namespace Volo.Abp.IdentityModel
this[nameof(UserName)] = userName; this[nameof(UserName)] = userName;
this[nameof(UserPassword)] = userPassword; this[nameof(UserPassword)] = userPassword;
this[nameof(RequireHttps)] = requireHttps.ToString().ToLowerInvariant(); this[nameof(RequireHttps)] = requireHttps.ToString().ToLowerInvariant();
this[nameof(CacheAbsoluteExpiration)] = cacheAbsoluteExpiration.ToString(CultureInfo.InvariantCulture);
} }
} }
} }

29
framework/src/Volo.Abp.IdentityModel/Volo/Abp/IdentityModel/IdentityModelAuthenticationService.cs

@ -78,13 +78,11 @@ namespace Volo.Abp.IdentityModel
public virtual async Task<string> GetAccessTokenAsync(IdentityClientConfiguration configuration) public virtual async Task<string> GetAccessTokenAsync(IdentityClientConfiguration configuration)
{ {
var tokenEndpoint = await GetTokenEndpoint(configuration);
var cacheKey = CalculateTokenCacheKey(configuration); var cacheKey = CalculateTokenCacheKey(configuration);
var tokenCacheItem = await TokenCache.GetAsync(cacheKey); var tokenCacheItem = await TokenCache.GetAsync(cacheKey);
if (tokenCacheItem == null) if (tokenCacheItem == null)
{ {
var tokenResponse = await GetTokenResponse(tokenEndpoint, configuration); var tokenResponse = await GetTokenResponse(configuration);
if (tokenResponse.IsError) if (tokenResponse.IsError)
{ {
@ -99,14 +97,12 @@ namespace Volo.Abp.IdentityModel
throw new AbpException(withoutInnerException[0]); throw new AbpException(withoutInnerException[0]);
} }
await TokenCache.SetAsync(cacheKey, new IdentityModelTokenCacheItem(tokenResponse.AccessToken), tokenCacheItem = new IdentityModelTokenCacheItem(tokenResponse.AccessToken);
new DistributedCacheEntryOptions() await TokenCache.SetAsync(cacheKey, tokenCacheItem,
new DistributedCacheEntryOptions
{ {
//Subtract 10 seconds of network request time. AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.CacheAbsoluteExpiration)
AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(tokenResponse.ExpiresIn - 10)
}); });
return tokenResponse.AccessToken;
} }
return tokenCacheItem.AccessToken; return tokenCacheItem.AccessToken;
@ -148,15 +144,14 @@ namespace Volo.Abp.IdentityModel
await DiscoveryDocumentCache.SetAsync(tokenEndpointUrlCacheKey, discoveryDocumentCacheItem, await DiscoveryDocumentCache.SetAsync(tokenEndpointUrlCacheKey, discoveryDocumentCacheItem,
new DistributedCacheEntryOptions new DistributedCacheEntryOptions
{ {
SlidingExpiration = TimeSpan.FromMinutes(30) AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(configuration.CacheAbsoluteExpiration)
}); });
} }
return discoveryDocumentCacheItem.TokenEndpoint; return discoveryDocumentCacheItem.TokenEndpoint;
} }
protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse( protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse(IdentityClientConfiguration configuration)
IdentityClientConfiguration configuration)
{ {
using (var httpClient = HttpClientFactory.CreateClient(HttpClientName)) using (var httpClient = HttpClientFactory.CreateClient(HttpClientName))
{ {
@ -173,10 +168,10 @@ namespace Volo.Abp.IdentityModel
} }
} }
protected virtual async Task<TokenResponse> GetTokenResponse( protected virtual async Task<TokenResponse> GetTokenResponse(IdentityClientConfiguration configuration)
string tokenEndpoint,
IdentityClientConfiguration configuration)
{ {
var tokenEndpoint = await GetTokenEndpoint(configuration);
using (var httpClient = HttpClientFactory.CreateClient(HttpClientName)) using (var httpClient = HttpClientFactory.CreateClient(HttpClientName))
{ {
AddHeaders(httpClient); AddHeaders(httpClient);
@ -217,9 +212,7 @@ namespace Volo.Abp.IdentityModel
return Task.FromResult(request); return Task.FromResult(request);
} }
protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync( protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync(string tokenEndpoint, IdentityClientConfiguration configuration)
string tokenEndpoint,
IdentityClientConfiguration configuration)
{ {
var request = new ClientCredentialsTokenRequest var request = new ClientCredentialsTokenRequest
{ {

Loading…
Cancel
Save