Table of Contents

Class AuthorizationCodeCredential

Namespace
Azure.Identity
Assembly
Azure.Identity.dll

Authenticates by redeeming an authorization code previously obtained from Microsoft Entra ID. See https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow for more information about the authorization code authentication flow.

public class AuthorizationCodeCredential : TokenCredential
Inheritance
AuthorizationCodeCredential

Constructors

AuthorizationCodeCredential()

Protected constructor for mocking.

protected AuthorizationCodeCredential()

AuthorizationCodeCredential(string, string, string, string)

Creates an instance of the ClientSecretCredential with the details needed to authenticate against Microsoft Entra ID with a prefetched authorization code.

public AuthorizationCodeCredential(string tenantId, string clientId, string clientSecret, string authorizationCode)

Parameters

tenantId string

The Microsoft Entra tenant (directory) ID of the service principal.

clientId string

The client (application) ID of the service principal

clientSecret string

A client secret that was generated for the App Registration used to authenticate the client.

authorizationCode string

The authorization code obtained from a call to authorize. The code should be obtained with all required scopes. See https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow for more information.

AuthorizationCodeCredential(string, string, string, string, AuthorizationCodeCredentialOptions)

Creates an instance of the ClientSecretCredential with the details needed to authenticate against Microsoft Entra ID with a prefetched authorization code.

public AuthorizationCodeCredential(string tenantId, string clientId, string clientSecret, string authorizationCode, AuthorizationCodeCredentialOptions options)

Parameters

tenantId string

The Microsoft Entra tenant (directory) ID of the service principal.

clientId string

The client (application) ID of the service principal

clientSecret string

A client secret that was generated for the App Registration used to authenticate the client.

authorizationCode string

The authorization code obtained from a call to authorize. The code should be obtained with all required scopes. See https://learn.microsoft.com/entra/identity-platform/v2-oauth2-auth-code-flow for more information.

options AuthorizationCodeCredentialOptions

Options that allow to configure the management of the requests sent to Microsoft Entra ID.

Methods

GetToken(TokenRequestContext, CancellationToken)

Obtains a token from Microsoft Entra ID, using the specified authorization code to authenticate. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.

public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = default)

Parameters

requestContext TokenRequestContext

The details of the authentication request.

cancellationToken CancellationToken

A CancellationToken controlling the request lifetime.

Returns

AccessToken

An Azure.Core.AccessToken which can be used to authenticate service client calls.

GetTokenAsync(TokenRequestContext, CancellationToken)

Obtains a token from Microsoft Entra ID, using the specified authorization code to authenticate. Acquired tokens are cached by the credential instance. Token lifetime and refreshing is handled automatically. Where possible, reuse credential instances to optimize cache effectiveness.

public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken = default)

Parameters

requestContext TokenRequestContext

The details of the authentication request.

cancellationToken CancellationToken

A CancellationToken controlling the request lifetime.

Returns

ValueTask<AccessToken>

An Azure.Core.AccessToken which can be used to authenticate service client calls.