The Azure Identity library provides Microsoft Entra ID token authentication support across the Azure SDK with a comprehensive set of TokenCredential implementations.
—
Legacy authentication mechanism using MSAL shared token cache, formerly used for Visual Studio integration. This credential provides compatibility with legacy token caches.
Provides token credentials from the MSAL shared token cache.
/**
* Shared token cache credential for legacy token cache integration
*/
class SharedTokenCacheCredential implements TokenCredential {
Mono<AccessToken> getToken(TokenRequestContext request);
// Note: Does not support synchronous getTokenSync method
}
class SharedTokenCacheCredentialBuilder extends AadCredentialBuilderBase<SharedTokenCacheCredentialBuilder> {
SharedTokenCacheCredentialBuilder username(String username);
SharedTokenCacheCredentialBuilder tokenCachePersistenceOptions(TokenCachePersistenceOptions tokenCachePersistenceOptions);
SharedTokenCacheCredentialBuilder authenticationRecord(AuthenticationRecord authenticationRecord);
SharedTokenCacheCredential build();
}Usage Examples:
import com.azure.identity.SharedTokenCacheCredential;
import com.azure.identity.SharedTokenCacheCredentialBuilder;
// Basic usage with username
TokenCredential credential = new SharedTokenCacheCredentialBuilder()
.clientId("your-client-id")
.tenantId("your-tenant-id")
.username("user@example.com")
.build();
// With authentication record for specific account
AuthenticationRecord record = // ... previously obtained record
TokenCredential recordCredential = new SharedTokenCacheCredentialBuilder()
.clientId("your-client-id")
.tenantId("your-tenant-id")
.authenticationRecord(record)
.build();
// Use with Azure SDK clients
SecretClient client = new SecretClientBuilder()
.vaultUrl("https://myvault.vault.azure.net/")
.credential(credential)
.buildClient();This credential was primarily designed for:
// With custom token cache settings
TokenCredential credential = new SharedTokenCacheCredentialBuilder()
.clientId("your-client-id")
.tenantId("your-tenant-id")
.username("user@example.com")
.tokenCachePersistenceOptions(new TokenCachePersistenceOptions()
.setName("MyLegacyTokenCache")
.setUnencryptedStorageAllowed(false))
.build();// Specify exact username to select account
TokenCredential credential = new SharedTokenCacheCredentialBuilder()
.username("john.doe@contoso.com")
.clientId("client-id")
.build();// Use authentication record for precise account selection
AuthenticationRecord record = AuthenticationRecord.deserialize(inputStream);
TokenCredential credential = new SharedTokenCacheCredentialBuilder()
.authenticationRecord(record)
.clientId("client-id")
.build();From SharedTokenCacheCredential to modern credentials:
// Legacy approach (not recommended)
TokenCredential legacyCredential = new SharedTokenCacheCredentialBuilder()
.username("user@example.com")
.clientId("client-id")
.build();
// Modern recommended approach
TokenCredential modernCredential = new DefaultAzureCredentialBuilder()
.build();
// Or for development scenarios
TokenCredential devCredential = new AzureCliCredentialBuilder()
.build();Common Issues:
Diagnostics:
// Enable detailed logging
TokenCredential credential = new SharedTokenCacheCredentialBuilder()
.username("user@example.com")
.clientId("client-id")
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.build();Instead of SharedTokenCacheCredential, consider:
DefaultAzureCredential - Comprehensive authentication chainInteractiveBrowserCredential - Modern interactive authenticationAzureCliCredential - Development environment authenticationVisualStudioCodeCredential - VS Code integration (if applicable)Throws CredentialUnavailableException when:
Install with Tessl CLI
npx tessl i tessl/maven-com-azure--azure-identitydocs