Class AuthorizationCodeTokenProvider
Provides bearer tokens from an OAuth 2.0 authorization code session. Caches the access token, refreshes it single-flight via the refresh token, and raises TokensUpdated after every acquisition so hosts can persist the (rotating) refresh token. Contains no browser interaction of its own, which makes it suitable for headless hosts such as Windows services — seed it with a stored refresh token and keep persisting rotations.
public class AuthorizationCodeTokenProvider : IGraphQlAuthProvider, IDisposable
- Inheritance
-
objectAuthorizationCodeTokenProvider
- Implements
Constructors
AuthorizationCodeTokenProvider(OAuthAuthorizationCodeClient, GraphQlTokenSet?, string?, Func<CancellationToken, Task<GraphQlTokenSet>>?, ILoggerFactory?)
Initializes a new instance of the AuthorizationCodeTokenProvider class.
public AuthorizationCodeTokenProvider(OAuthAuthorizationCodeClient oauthClient, GraphQlTokenSet? initialTokens = null, string? initialRefreshToken = null, Func<CancellationToken, Task<GraphQlTokenSet>>? reauthenticateAsync = null, ILoggerFactory? loggerFactory = null)
Parameters
oauthClientOAuthAuthorizationCodeClientThe protocol client used to refresh tokens.
initialTokensGraphQlTokenSetOptional token set from a completed login (e.g. InteractiveBrowserLogin).
initialRefreshTokenstringOptional stored refresh token to resume a session without a new login. Ignored when
initialTokenscarries a refresh token.reauthenticateAsyncFunc<CancellationToken, Task<GraphQlTokenSet>>Optional callback invoked when no valid refresh token is available (absent, expired, or revoked) — e.g. to re-run an interactive login. If null, token acquisition fails with a GraphQlAuthenticationException in that situation.
loggerFactoryILoggerFactoryOptional logger factory. If null, SDK logging is disabled for this provider.
Properties
CurrentTokens
The current token set, if any (e.g. to read the refresh token for persistence).
public GraphQlTokenSet? CurrentTokens { get; }
Property Value
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetTokenAsync(CancellationToken)
Returns the current bearer token.
public Task<string> GetTokenAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenToken to cancel the operation (e.g. a token-refresh request).
Returns
Events
TokensUpdated
Raised after every token acquisition or refresh. Subscribe to persist RefreshToken — refresh tokens rotate, so always store the latest one. Handlers run while the refresh lock is held and must be quick and non-blocking; handler exceptions are logged and never propagated to the caller.
public event Action<GraphQlTokenSet>? TokensUpdated