3
votes

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
And you made sure the token expiry time is immediately? What I'm saying is in the screenshot the time is 00:08:30 UTC, just wanna check you are not confused with timezones etc. - juunas
You were right on point @juunas I was not aware at the time that the tokens were in UTC time. I spend the evening making console app to consume the token; I was expecting to get expired token error but it worked just fine, thanks for point me in the right direction 😊 - kelvinmac

1 Answers

0
votes

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.