Class ChainedTokenCredential

java.lang.Object
com.azure.identity.ChainedTokenCredential
All Implemented Interfaces:
com.azure.core.credential.TokenCredential
Direct Known Subclasses:
DefaultAzureCredential

public class ChainedTokenCredential extends Object implements com.azure.core.credential.TokenCredential
The ChainedTokenCredential is a convenience credential that allows users to chain together a set of TokenCredential together. The credential executes each credential in the chain sequentially and returns the token from the first credential in the chain that successfully authenticates.

Sample: Construct a ChainedTokenCredential.

The following code sample demonstrates the creation of a ChainedTokenCredential, using the ChainedTokenCredentialBuilder to configure it. The sample below tries silent username+password login tried first, then interactive browser login as needed (e.g. when 2FA is turned on in the directory). Once this credential is created, it may be passed into the builder of many of the Azure SDK for Java client builders as the 'credential' parameter.

 TokenCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
     .clientId(clientId)
     .username(fakeUsernamePlaceholder)
     .password(fakePasswordPlaceholder)
     .build();
 TokenCredential interactiveBrowserCredential = new InteractiveBrowserCredentialBuilder()
     .clientId(clientId)
     .port(8765)
     .build();
 TokenCredential credential = new ChainedTokenCredentialBuilder()
     .addLast(usernamePasswordCredential)
     .addLast(interactiveBrowserCredential)
     .build();
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Mono<com.azure.core.credential.AccessToken>
    getToken(com.azure.core.credential.TokenRequestContext request)
    Sequentially calls TokenCredential.getToken(TokenRequestContext) on all the specified credentials, returning the first successfully obtained AccessToken.
    com.azure.core.credential.AccessToken
    getTokenSync(com.azure.core.credential.TokenRequestContext request)
    Synchronously get a token for a given resource/audience.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getToken

      public Mono<com.azure.core.credential.AccessToken> getToken(com.azure.core.credential.TokenRequestContext request)
      Sequentially calls TokenCredential.getToken(TokenRequestContext) on all the specified credentials, returning the first successfully obtained AccessToken. This method is called automatically by Azure SDK client libraries. You may call this method directly, but you must also handle token caching and token refreshing.
      Specified by:
      getToken in interface com.azure.core.credential.TokenCredential
      Parameters:
      request - the details of the token request
      Returns:
      a Publisher that emits a single access token
    • getTokenSync

      public com.azure.core.credential.AccessToken getTokenSync(com.azure.core.credential.TokenRequestContext request)
      Description copied from interface: com.azure.core.credential.TokenCredential
      Synchronously get a token for a given resource/audience. This method is called automatically by Azure SDK client libraries. You may call this method directly, but you must also handle token caching and token refreshing.
      Specified by:
      getTokenSync in interface com.azure.core.credential.TokenCredential
      Parameters:
      request - the details of the token request
      Returns:
      The Access Token