Class AzurePowerShellCredential

java.lang.Object
com.azure.identity.AzurePowerShellCredential
All Implemented Interfaces:
com.azure.core.credential.TokenCredential

public class AzurePowerShellCredential extends Object implements com.azure.core.credential.TokenCredential

The Azure Powershell is a command-line tool that allows users to manage Azure resources from their local machine or terminal. It allows users to authenticate interactively as a user and/or a service principal against Microsoft Entra ID. The AzurePowershellCredential authenticates in a development environment and acquires a token on behalf of the logged-in user or service principal in Azure Powershell. It acts as the Azure Powershell logged in user or service principal and executes an Azure Powershell command underneath to authenticate the application against Microsoft Entra ID.

Configure AzurePowershellCredential

To use this credential, the developer needs to authenticate locally in Azure Powershell using one of the commands below:

  1. Run "Connect-AzAccount" in Azure Powershell to authenticate as a user.
  2. Run "Connect-AzAccount -ServicePrincipal -ApplicationId {servicePrincipalId} -Tenant {tenantId} -CertificateThumbprint {thumbprint} to authenticate as a service principal."

You may need to repeat this process after a certain time period, depending on the refresh token validity in your organization. Generally, the refresh token validity period is a few weeks to a few months. AzurePowershellCredential will prompt you to sign in again.

Sample: Construct AzurePowershellCredential

The following code sample demonstrates the creation of a AzurePowerShellCredential, using the AzurePowerShellCredentialBuilder to configure it. 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 powerShellCredential = new AzurePowerShellCredentialBuilder()
     .build();
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Mono<com.azure.core.credential.AccessToken>
    getToken(com.azure.core.credential.TokenRequestContext request)
    Asynchronously 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

    Methods inherited from interface com.azure.core.credential.TokenCredential

    getTokenSync
  • Method Details

    • getToken

      public Mono<com.azure.core.credential.AccessToken> getToken(com.azure.core.credential.TokenRequestContext request)
      Description copied from interface: com.azure.core.credential.TokenCredential
      Asynchronously 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:
      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