Access token from ARM expired when received

Viewed 256

I'm creating an app that creates scheduled jobs programmatically, I am therefore using the REST API provided by Azure. I require an access token but when I authenticate, the access token returned from request expires that same time it is sent to me.

The client id is an application that I registered on Azure AD and copied the Application ID.

I created the secret key in the Keys blade.

I am using the AzureEnvironment.AzureGlobalCloud.AuthenticationEndpoint/{My Azure ID} as the authority (https://login.microsoftonline.com/) I am requesting the access token for Azure Resource manager using AzureEnvironment.AzureGlobalCloud.ResourceManagerEndpoint.

private async Task<string> GetAccessToken()
{
    _authContext = new AuthenticationContext($"{AzureEnvironment.AzureGlobalCloud.AuthenticationEndpoint}{ _jobConfig.TenantId}");
    var credential = new ClientCredential(_jobConfig.AzureApplicationId, _jobConfig.AzureApplicationSecret);
    var authResult = await _authContext.AcquireTokenAsync(AzureEnvironment.AzureGlobalCloud.ResourceManagerEndpoint, credential);

    return authResult.AccessToken;
}

_jobconfig is an instance containing the data mentioned above and referenced in the code.

Debug Data

1 Answers

The expiry time is display in Coordinated Universal Time (UTC), so make sure to see what that is in your timezone.

It would be a bug in Azure AD's token endpoint if it was expiring immediately. The token might still work though at first because token validation often allows a little bit of clock skew.

Related