Share via


ChainedTokenCredential Class

Definition

Provides a TokenCredential implementation which chains multiple TokenCredential implementations to be tried in order until one of the GetToken methods returns a non-default AccessToken. For more information, see ChainedTokenCredential overview.

public class ChainedTokenCredential : Azure.Core.TokenCredential
type ChainedTokenCredential = class
    inherit TokenCredential
Public Class ChainedTokenCredential
Inherits TokenCredential
Inheritance
ChainedTokenCredential

Examples

The following example demonstrates creating a credential which will attempt to authenticate using managed identity and fall back to Azure CLI for authentication if a managed identity is unavailable in the current environment.

// Authenticate using managed identity if it is available; otherwise use the Azure CLI to authenticate.

var credential = new ChainedTokenCredential(new ManagedIdentityCredential(), new AzureCliCredential());

var eventHubProducerClient = new EventHubProducerClient("myeventhub.eventhubs.windows.net", "myhubpath", credential);

Constructors

ChainedTokenCredential()

Protected constructor for mocking.

ChainedTokenCredential(TokenCredential[])

Creates an instance with the specified TokenCredential sources.

Methods

GetToken(TokenRequestContext, CancellationToken)

Sequentially calls GetToken(TokenRequestContext, CancellationToken) on all the specified sources, returning the first successfully obtained AccessToken. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.

GetTokenAsync(TokenRequestContext, CancellationToken)

Sequentially calls GetToken(TokenRequestContext, CancellationToken) on all the specified sources, returning the first successfully obtained AccessToken. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.

Applies to