SPN Access Token sync delay - Azure (AAD)

Ash 125 Reputation points Microsoft Employee
2025-07-29T18:40:37.16+00:00

Hi everyone,

I'm encountering a puzzling issue in my Azure DevOps pipeline and would appreciate any insights or suggestions.

🔧 Setup Overview:

In the pipeline, I first run a custom .NET extension that fetches an Azure access token using Service Principal credentials (AppId, AppKey, TenantId). Here's a simplified snippet:

var spnAccessTokenResponse = await retryPolicy.ExecuteAsync(() =>
    azureTokenClient.TryFetchAzureAccessTokenUsingSPN(
        credentialFetchTaskReqResponse.TenantId,
        credentialFetchTaskReqResponse.AppId,
        credentialFetchTaskReqResponse.AppKey)).ConfigureAwait(false);
                

After successfully retrieving the token, I proceed to run an Azure PowerShell task using a service connection configured with the same SPN credentials.

⚠️ Problem:

Even though the token fetch succeeds in the .NET task, the Azure PowerShell task intermittently retries due to what appears to be an AAD sync delay.

🧪 What I've Tried:

  • Added retry logic with exponential backoff in the .NET code to improve resilience.PR - Aad Sync Issue - Adding retry to Get Credential... - DeploymentStd1B 4344234
  • Validated that the service connection is correctly configured and matches the SPN used in the code.
  • Confirmed that the token is valid and usable immediately after retrieval.

💡 Question:

Is there a known workaround or best practice to ensure that the Azure PowerShell task doesn’t hit these sync-related retries?

Any help or guidance would be greatly appreciated!.

Azure DevOps
{count} votes

2 answers

Sort by: Most helpful
  1. Durga Reshma Malthi 9,840 Reputation points Microsoft External Staff Moderator
    2025-07-30T07:45:43.1733333+00:00

    Hi Ash

    Could you please follow the below steps:

    • You can add this delay line in your above snippet, so it will help for AAD sync delays.
        await Task.Delay(TimeSpan.FromSeconds(10));
      
    • or alternatively, add some delay between the token retrieval in your .NET task and the execution of the azure PowerShell task.
        - task: PowerShell@2
          displayName: 'Delay'
          inputs:
            targetType: 'inline'
            script: 'Start-Sleep -Seconds 20'
      
      This will help for AAD sync delays
    • Also ensure that the service connection in Azure DevOps is configured to use the same credentials as the SPN used in your .NET code.

    Hope this helps!

    Please Let me know if you have any queries.

    1 person found this answer helpful.
    0 comments No comments

  2. Durga Reshma Malthi 9,840 Reputation points Microsoft External Staff Moderator
    2025-07-31T08:38:32.2766667+00:00

    Hi Ash

    It could be due to some of the below reasons:

    • Access tokens have a limited lifespan typically like 1 hour. If your pipeline runs multiple tasks that require authentication, it's possible that the token fetched initially is still valid for the first task but expires by the time subsequent tasks are executed. This could lead to failures in those later tasks if they attempt to use an expired token.
    • As you mentioned, AAD can experience replication delays. When you create or modify service principals, it may take some time for those changes to propagate across all AAD partitions. If your .NET code successfully fetches a token immediately after a change, but the Azure PowerShell task runs shortly after, it might hit a partition that hasn't yet received the updated information.
    • Yes, the access token is being fetched from different partitions each time because Azure AD is distributed and does not guarantee instant consistency across partitions.
    • And also even though your .NET task gets a valid token, a subsequent PowerShell task might hit a different partition that hasn't fully synced yet.

    Hope this helps!

    Please Let me know if you have any queries.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.