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.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<bool>() ?? true;
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(
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);
}
}
}
}

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)
{
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<DiscoveryDocumentResponse> GetDiscoveryResponse(
IdentityClientConfiguration configuration)
protected virtual async Task<DiscoveryDocumentResponse> GetDiscoveryResponse(IdentityClientConfiguration configuration)
{
using (var httpClient = HttpClientFactory.CreateClient(HttpClientName))
{
@ -173,10 +168,10 @@ namespace Volo.Abp.IdentityModel
}
}
protected virtual async Task<TokenResponse> GetTokenResponse(
string tokenEndpoint,
IdentityClientConfiguration configuration)
protected virtual async Task<TokenResponse> 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<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync(
string tokenEndpoint,
IdentityClientConfiguration configuration)
protected virtual Task<ClientCredentialsTokenRequest> CreateClientCredentialsTokenRequestAsync(string tokenEndpoint, IdentityClientConfiguration configuration)
{
var request = new ClientCredentialsTokenRequest
{

Loading…
Cancel
Save