Class AbstractAuthenticationService<TClient>
- Namespace
- Visus.AddressValidation.Services
- Assembly
- Visus.AddressValidation.dll
Abstract base class for an authentication service that obtains and caches access tokens via an IAuthenticationClient implementation.
public abstract class AbstractAuthenticationService<TClient> where TClient : IAuthenticationClient
Type Parameters
TClientThe type of IAuthenticationClient used to request access tokens.
- Inheritance
-
AbstractAuthenticationService<TClient>
- Inherited Members
Constructors
AbstractAuthenticationService(TClient, HybridCache)
Initializes a new instance of AbstractAuthenticationService<TClient>.
protected AbstractAuthenticationService(TClient authenticationClient, HybridCache cache)
Parameters
authenticationClientTClientThe IAuthenticationClient used to request access tokens.
cacheHybridCacheThe HybridCache used to cache access tokens.
Exceptions
- ArgumentNullException
Thrown when
authenticationClientorcacheis null.
Fields
CacheKeyTag
Prefix applied to all cache keys produced by this service to namespace them within the shared cache.
protected const string CacheKeyTag = "vs-ave-auth:"
Field Value
Methods
GenerateCacheKey()
Generates a unique cache key to cache retrieved access tokens.
protected abstract string GenerateCacheKey()
Returns
- string
A non-null, non-empty string containing only A-Z, a-z, 0-9, underscores, hyphens, and colons.
Exceptions
- InvalidOperationException
Thrown when the credentials needed to build the key are absent or invalid, or when the key contains disallowed characters.
GetAccessTokenAsync(CancellationToken)
Retrieves an access token using token-based authentication.
public Task<string?> GetAccessTokenAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA token that cancels the operation.
Returns
- Task<string>
The current access token, or null if the authentication service returns no valid token.
Remarks
This method caches the access token in HybridCache. When no cached entry
exists, it fetches a new token via
RequestClientCredentialsTokenAsync(CancellationToken). It stores the
token with an expiration of ExpiresIn - 60 seconds, as a safety buffer before the
token expires.
Subsequent calls return the cached token until it expires. At that point, this method fetches and caches a new token. If the authentication service returns an invalid or empty token, this method removes the cache entry and returns null.