Table of Contents

Class ClientCredentialsTokenHandler

Namespace
Fdyay.AccessTokenManagement
Assembly
Fdyay.AccessTokenManagement.dll

Attaches a client-credentials access token to every outbound request.

public sealed class ClientCredentialsTokenHandler : DelegatingHandler, IDisposable
Inheritance
ClientCredentialsTokenHandler
Implements
Inherited Members

Remarks

The one retry is the interesting part. A cached token can be refused for reasons the cache cannot see — revoked, or signed by a key that has since rotated — and it will keep being refused until it expires on its own, which for a long-lived token is a long outage for something already fixed at the other end. So a 401 evicts the token and the request is sent once more with a fresh one.

Exactly once. A second 401 with a token minted seconds ago is not a stale credential; it is the resource server saying no, and retrying it again turns one refusal into a loop.

Constructors

ClientCredentialsTokenHandler(IClientCredentialsTokenService, ClientCredentialsClientName)

Creates a handler that attaches the named client's tokens.

public ClientCredentialsTokenHandler(IClientCredentialsTokenService tokenService, ClientCredentialsClientName clientName)

Parameters

tokenService IClientCredentialsTokenService

Where the tokens come from, and where a refused one is evicted.

clientName ClientCredentialsClientName

The registered client whose tokens are attached.

Methods

SendAsync(HttpRequestMessage, CancellationToken)

Sends the request with the client's access token in its Authorization header, and on a 401 evicts the token and sends it once more with a fresh one.

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

Parameters

request HttpRequestMessage

The outbound request.

cancellationToken CancellationToken

Cancels the token acquisition and the send.

Returns

Task<HttpResponseMessage>

The response; after a retry, the second response.

Exceptions

ArgumentNullException

request is null.