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 for caching 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 required credentials needed to construct the key are absent or invalid, or when the returned key contains characters outside the allowed set.
GetAccessTokenAsync(CancellationToken)
Retrieves an access token using token-based authentication.
public Task<string?> GetAccessTokenAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token that can be used to cancel the work.
Returns
- Task<string>
The current access token returned by the authentication service, or null if the authentication service did not return a valid token.
Remarks
The access token is cached using HybridCache. When no cached entry exists,
a new token is fetched via RequestClientCredentialsTokenAsync(CancellationToken)
and stored in the cache with an expiration of ExpiresIn - 60 seconds to provide a
safety buffer before the token actually expires.
Subsequent calls return the cached token until it expires, at which point a new token is fetched and cached. If the authentication service returns an invalid or empty token, the cache entry is removed and null is returned.